Note that there are some explanatory texts on larger screens.

plurals
  1. POAJAX request callback using jQuery
    primarykey
    data
    text
    <p>I am new to the use of jQuery for handling AJAX, and have written a basic script to get the basics down. Currently I am POSTing an AJAX request to the same file, and I wish to do some additional processing based on the results of that AJAX call. </p> <p>Here is my code:</p> <pre><code>**/*convertNum.php*/** $num = $_POST['json']; if (isset($num)) echo $num['number'] * 2; ?&gt; &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; td {border:none;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;table width="800" border="1"&gt; &lt;tr&gt; &lt;td align="center"&gt;Number To Send&lt;br /&gt;&lt;input type="text" id="numSend" size="40%" style="border:2px solid black;"&gt;&lt;/td&gt; &lt;td align="center"&gt;Number Returned&lt;br /&gt;&lt;input type="text" id="numReturn" size="40%" readonly&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td align="center" colspan="4"&gt;&lt;input type="button" value="Get Number" id="getNum" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;script&gt; $(document).ready(function () { $('#getNum').click(function () { var $numSent = $('#numSend').val(); var json = {"number":$numSent}; $.post("convertNum.php", {"json": json}).done(function (data) { alert(data); } ); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here is the response I get if I submit the number '2':</p> <pre><code>4 &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; td {border:none;} &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;table width="800" border="1"&gt; &lt;tr&gt; &lt;td align="center"&gt;Number To Send&lt;br /&gt;&lt;input type="text" id="numSend" size="40%" style="border:2px solid black;"&gt;&lt;/td&gt; &lt;td align="center"&gt;Number Returned&lt;br /&gt;&lt;input type="text" id="numReturn" size="40%" readonly&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;td align="center" colspan="4"&gt;&lt;input type="button" value="Get Number" id="getNum" /&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt; &lt;script&gt; $(document).ready(function () { $('#getNum').click(function () { var $numSent = $('#numSend').val(); var json = {"number":$numSent}; $.post("convertNum.php", {"json": json}).done(function (data) { alert(data); } ); }); }); &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Obviously I'm only interested in receiving and using the number '4', hence my question: <strong>What is the best way to specify exactly what data I want returned?</strong></p> <p>Some thoughts I've had:</p> <ul> <li>wrapping all my HTML inside a if statement (i.e., if $num isset, do NOT output html; else output HTML) but then I'm echoing HTML, and I'd rather not do that.</li> <li>Setting up a separate PHP script to receive my AJAX call: That was what I did originally, and it works just fine. However, I am interested in keeping everything inside one file, and wanted to explore possible ways of doing so.</li> </ul> <p>I'm sure there is an elegant way to do this. Thanks for any suggestions!</p>
    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.
 

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