Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is a <a href="http://www.w3schools.com/PHP/php_ajax_database.asp" rel="nofollow noreferrer">good example</a>, it shows a <code>SELECT</code> statement but it should be straight forward and easy to update the script to what you need.</p> <p><strong>HTML from the example page:</strong></p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script src="selectuser.js"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form&gt; Select a User: &lt;select name="users" onchange="showUser(this.value)"&gt; &lt;option value="1"&gt;Peter Griffin&lt;/option&gt; &lt;option value="2"&gt;Lois Griffin&lt;/option&gt; &lt;option value="3"&gt;Glenn Quagmire&lt;/option&gt; &lt;option value="4"&gt;Joseph Swanson&lt;/option&gt; &lt;/select&gt; &lt;/form&gt; &lt;p&gt; &lt;div id="txtHint"&gt;&lt;b&gt;User info will be listed here.&lt;/b&gt;&lt;/div&gt; &lt;/p&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p><strong>javaScript:</strong></p> <pre><code>var xmlHttp; function showUser(str) { xmlHttp=GetXmlHttpObject(); if (xmlHttp==null) { alert ("Browser does not support HTTP Request"); return; } var url="getuser.php"; url=url+"?q="+str; url=url+"&amp;sid="+Math.random(); xmlHttp.onreadystatechange=stateChanged; xmlHttp.open("GET",url,true); xmlHttp.send(null); } function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { document.getElementById("txtHint").innerHTML=xmlHttp.responseText; } } function GetXmlHttpObject() { var xmlHttp=null; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { //Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } } return xmlHttp; } </code></pre> <p><strong>PHP called by Ajax:</strong></p> <pre><code>&lt;?php $q = $_GET["q"]; $con = mysql_connect('localhost', 'peter', 'abc123'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("ajax_demo", $con); $sql = "SELECT * FROM user WHERE id = '" . $q . "'"; $result = mysql_query($sql); echo "&lt;table border='1'&gt; &lt;tr&gt; &lt;th&gt;Firstname&lt;/th&gt; &lt;th&gt;Lastname&lt;/th&gt; &lt;th&gt;Age&lt;/th&gt; &lt;th&gt;Hometown&lt;/th&gt; &lt;th&gt;Job&lt;/th&gt; &lt;/tr&gt;"; while ($row = mysql_fetch_array($result)) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['LastName'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['Age'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['Hometown'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['Job'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; mysql_close($con); ?&gt; </code></pre>
 

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