Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting Ajax to execute using PHP generated values
    primarykey
    data
    text
    <p>I'm completely new to AJAX, and am trying to use it to grab the details of sample users once said user is selected from a dropdown list generated via a php/mysql query. The process works fine when the dropdown values are hardcoded (<code>&lt;option vlaue="1"&gt;1&lt;/option&gt;, &lt;option vlaue="2"&gt;2&lt;/option&gt;, &lt;option value="3"&gt;3&lt;/option&gt;</code>, etc.), but nothing happens when I use the PHP version (snippet 2 below). I'm obviously missing out on some concept here, as I've Googled this every which way I can, as well as searched through Stackoverflow's archives, but can't find an answer. There's plenty on using AJAX to generate the list itself, but not on using it to grab PHP vars.</p> <p>Any help/advice hugely appreciated.</p> <p>The following code snips are:</p> <ol> <li>The header javascript, forming the object </li> <li>The form itself </li> <li>The PHP action script</li> </ol> <p><code><pre> function showUser(str) { if (str=="") { document.getElementById("txtHint").innerHTML=""; return; } if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange=function() { if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200) { document.getElementById("txtHint").innerHTML=xmlhttp.responseText; } } xmlhttp.open("GET","tester-engine.php?q="+str,true); xmlhttp.send(); } </pre></code></p> <p>The form itself (db connection established):</p> <pre><code>&lt;form&gt; &lt;select name="profile-owner" onchange="showUser(this.value)"&gt; &lt;?php $contactresult = mysql_query("SELECT * FROM contacts"); while ($row = mysql_fetch_array($contactresult)) { echo '&lt;option value="' . $row['ContactID'] . '"&gt;' . $row['ContactLastName'] . ', ' . $row['ContactFirstName'] . '&lt;/option&gt;'; } &lt;/form&gt; &lt;div id="txtHint"&gt;&lt;b&gt;Person info will be listed here.&lt;/b&gt;&lt;/div&gt; </code></pre> <p>And, finally, the proccessing script (tester-engine.php):</p> <pre><code>&lt;?php $q=$_GET["q"]; $con = mysql_connect('####', '####', '####'); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("####", $con); $sql="SELECT * FROM contacts WHERE ContactID = '".$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;/tr&gt;"; while($row = mysql_fetch_array($result)) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['ContactFirstName'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['ContactLastName'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; mysql_close($con); ?&gt; </code></pre> <p>Thanks in Advance!</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.
    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