Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to retrieve data using jQuery.post
    primarykey
    data
    text
    <p>I'm trying to use <a href="http://docs.jquery.com/Ajax/jQuery.post" rel="nofollow noreferrer">jQuery.post()</a> function to retrieve some data. But i get no output.</p> <p>I have a HTML that displays a table. Clicking this table should trigger a jQuery.post event.</p> <p>My scriptfile looks like this:</p> <pre><code>jQuery(document).ready(function() { jQuery('#storeListTable tr').click(function() { var storeID = this.cells[0].innerHTML; //This gets me the rowID for the DB call. jQuery.post("../functions.php", { storeID: "storeID" }, function(data){ alert(data.name); // To test if I get any output }, "json"); }); }); </code></pre> <p>My PHP file looks like this: </p> <pre><code>&lt;?php inlcude_once('dal.php'); //Get store data, and ouput it as JSON. function getStoreInformation($storeID) { $storeID = "9";//$_GET["storeID"]; $sl = new storeLocator(); $result = $sl-&gt;getStoreData($storeID); while ($row = mysql_fetch_assoc($result)) { { $arr[] = $row; } $storeData = json_encode($arr); echo $storeData; //Output JSON data } ?&gt; </code></pre> <p>I have tested the PHP file, and it outputs the data in JSON format. My only problem now is to return this data to my javascript.</p> <ol> <li>since the javascript is located in the /js/ folder, is it correct to call the php file by using '../'?</li> <li>I don't think I'm passing the storeID parameter correctly. What is the right way?</li> <li>How can I call the getStoreInformation($storeID) function and pass on the parameter? The jQuery example on jQuery.com has the following line: $.post("test.php", { func: "getNameAndTime" } Is the getNameAndTime the name of the function in test.php ?</li> </ol> <hr> <p>I have gotten one step further. I have moved the code from inside the function(), to outside. So now the php code is run when the file is executed.</p> <p>My js script now looks like this:</p> <pre><code> jQuery('#storeListTable tr').click(function() { var storeID = this.cells[0].innerHTML; jQuery.post("get_storeData.php", { sID: storeID }, function(data){ alert(data); }, "text"); }); </code></pre> <p>This results in an alert window which ouputs the store data as string in JSON format. (because I have changed "json" to "text").</p> <p>The JSON string looks like this:</p> <pre><code>[{"id":"9","name":"Brandstad Byporten","street1":"Jernbanetorget","street2":null,"zipcode":"0154","city":"Oslo","phone":"23362011","fax":"22178889","www":"http:\/\/www.brandstad.no","email":"bs.byporten@brandstad.no","opening_hours":"Man-Fre 10-21, L","active":"pending"}] </code></pre> <p>Now, what I really want, is to ouput the data from JSON. So I would change "text" to "json" and "alert(data)" to "alert(data.name)". So now my js script will look like this:</p> <pre><code> jQuery('#storeListTable tr').click(function() { var storeID = this.cells[0].innerHTML; jQuery.post("get_storeData.php", { sID: storeID }, function(data){ alert(data.name); }, "json"); }); </code></pre> <p>Unfortunately, the only output I get, is "Undefined". And if I change "alert(data.name);" to "alert(data);", the output is "[object Object]".</p> <ol> <li><p>So how do I output the name of teh store?</p></li> <li><p>In the PHP file, I've tried setting $storeID = $_GET["sID"]; But I don't et the value. How can I get the value that is passed as paramter in jQuery.post ? (currently I have hardcoded the storeID, for testing)</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload