Note that there are some explanatory texts on larger screens.

plurals
  1. POPrinting JSON array to screen via ajax to php script
    primarykey
    data
    text
    <p>I've been comparing my code to other examples on the web and I still can't find my errors. When I load the page and click submit, nothing happens on the screen. However, in Firebug I receive the <code>POST 200 OK</code> and the PHP script that should be on screen is spelled out in the POST response tab. </p> <p>Since Firebug is responding appropriately, I am confused as to what is wrong.</p> <p>Basic HTML form</p> <pre><code>&lt;form id="form" action="" method="post"&gt; &lt;input type="submit" name="submit" id="submit" value="submit"/&gt; &lt;/form&gt; &lt;div id="results"&gt;&lt;/div&gt; </code></pre> <p>jQuery creates a JS object. The object is sent through <code>JSON.stringify</code> and <code>JSON.parse</code>. The submit event handler fires off the <code>$.ajax</code>. JSON data is passed to <code>ok.php</code> and it should return the PHP info called in the script, in theory.</p> <pre><code>var addIt = new Object(); addIt.one = "one"; addIt.two = 2; addIt.three = addIt.one +" + "+ addIt.two +" = "+ "three"; $jsonAddIt = JSON.stringify(addIt); $jsonAddIt = JSON.parse($jsonAddIt); $('#submit').click(function(e){ e.preventDefault(); $.ajax({ type: 'POST', url: 'ok.php', dataType:'json', data: ({ json:$jsonAddIt }), success:function(data) { $("#results").html(data); } }); }); </code></pre> <p>PHP</p> <pre><code>&lt;?php $ajaxInfo = $_POST["json"]; if ($ajaxInfo !="") { echo "info transfered"; } else echo "nothing"; ?&gt; &lt;div id="returned"&gt; &lt;?php print_r($ajaxInfo); ?&gt; &lt;/div&gt; </code></pre>
    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