Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing $.getJSON method and PHP
    primarykey
    data
    text
    <p>I am trying to use the $.getJSON function to return data. I have the following html file named page.html</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd "&gt; &lt;html xmlns=" http://www.w3.org/1999/xhtml "&gt; &lt;head&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;title&gt;Request json test&lt;/title&gt; &lt;script src="http://code.jquery.com/jquery-1.7.2.min.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function(){ $("button").click(function(){ $.getJSON("json-data.php",function(result){ alert(result); }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;button&gt;Get JSON data&lt;/button&gt; &lt;div id="showdata"&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>I also have the following PHP file named json-data.php:</p> <pre><code>&lt;?php //request data from the database //code here to connect to database and get the data you want /* Example JSON format { "item1": "I love jquery4u", "item2": "You love jQuery4u", "item3": "We love jQuery4u" } */ $item1 = "I love jquery4u"; $item2 = "You love jquery4u"; $item3 = "We love jquery4u"; //return in JSON format echo "{"; echo "item1: ", json_encode($item1), "\n"; echo "item2: ", json_encode($item2), "\n"; echo "item3: ", json_encode($item3), "\n"; echo "}"; ?&gt; </code></pre> <p>I am only trying to use the alert function to see the data in the PHP file, however it does not work. Anyone know why?</p> <p>Thanks, Jim</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    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.
 

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