Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy wont this ajax script work on wamp localhost
    primarykey
    data
    text
    <p>I was trying out this ajax example on my local machine running windows 7 home premium, apache, php and mysql, but it wont return any results to the browser. After reading a few articles here, I downloaded firebug, and from the firebug console->All what I get is this:</p> <pre><code>GET http://localhost/dev/ajax/ajax-example.php?age=100&amp;wpm=100&amp;sex=m 200 OK 1.01s </code></pre> <p>Response</p> <pre><code>Query: SELECT * FROM ajax_example WHERE sex = 'm' AND age &lt;= 100 AND wpm &lt;= 100 &lt;br /&gt;&lt;table&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Age&lt;/th&gt;&lt;th&gt;Sex&lt;/th&gt;&lt;th&gt;WPM&lt;/th&gt; &lt;/tr&gt;&lt;tr&gt;td&gt;Frank&lt;/td&gt;&lt;td&gt;45&lt;/td&gt;&lt;td&gt;m&lt;/td&gt;&lt;td&gt;87&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Regis&lt;/td&gt; &lt;td&gt;75&lt;/td&gt;&lt;td&gt;m&lt;/td&gt;&lt;td&gt;44&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt; </code></pre> <p>This thing above is what should be going to the broswer. The script seems to be working fine then, except the div wont refresh with new content.</p> <p>Is this a broswer issue or windows/javascript issue. What do I need to do to get this working? Could you please help.</p> <p>Here's the tutorial page I got all this from.</p> <pre><code>http://www.tutorialspoint.com/ajax/ajax_database.htm </code></pre> <p>This page is ajax.html</p> <pre><code>&lt;html&gt; &lt;body&gt; &lt;script language="javascript" type="text/javascript"&gt; &lt;!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); }catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data // sent from the server and will update // div section in the same page. ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('ajaxDiv'); ajaxDisplay.value = ajaxRequest.responseText; } } // Now get the value from user and pass it to // server script. var age = document.getElementById('age').value; var wpm = document.getElementById('wpm').value; var sex = document.getElementById('sex').value; var queryString = "?age=" + age ; queryString += "&amp;wpm=" + wpm + "&amp;sex=" + sex; ajaxRequest.open("GET", "ajax-example.php" + queryString, true); ajaxRequest.send(null); } //--&gt; &lt;/script&gt; &lt;form name="myForm"&gt; Max Age: &lt;input type="text" id="age" /&gt; &lt;br /&gt; Max WPM: &lt;input type="text" id="wpm" /&gt; &lt;br /&gt; Sex: &lt;select id="sex"&gt; &lt;option value="m"&gt;m&lt;/option&gt; &lt;option value="f"&gt;f&lt;/option&gt; &lt;/select&gt; &lt;input type="button" onclick="ajaxFunction()" value="Query MySQL"/&gt; &lt;/form&gt; &lt;div id="ajaxDiv"&gt;Your result will display here&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>This page is ajax-example.php</p> <pre><code>&lt;?php $dbhost = "localhost"; $dbuser = "root"; $dbpass = "norman"; $dbname = "test"; //Connect to MySQL Server mysql_connect($dbhost, $dbuser, $dbpass); //Select Database mysql_select_db($dbname) or die(mysql_error()); // Retrieve data from Query String $age = $_GET['age']; $sex = $_GET['sex']; $wpm = $_GET['wpm']; // Escape User Input to help prevent SQL Injection $age = mysql_real_escape_string($age); $sex = mysql_real_escape_string($sex); $wpm = mysql_real_escape_string($wpm); //build query $query = "SELECT * FROM ajax_example WHERE sex = '$sex'"; if(is_numeric($age)) $query .= " AND age &lt;= $age"; if(is_numeric($wpm)) $query .= " AND wpm &lt;= $wpm"; //Execute query $qry_result = mysql_query($query) or die(mysql_error()); //Build Result String $display_string = "&lt;table&gt;"; $display_string .= "&lt;tr&gt;"; $display_string .= "&lt;th&gt;Name&lt;/th&gt;"; $display_string .= "&lt;th&gt;Age&lt;/th&gt;"; $display_string .= "&lt;th&gt;Sex&lt;/th&gt;"; $display_string .= "&lt;th&gt;WPM&lt;/th&gt;"; $display_string .= "&lt;/tr&gt;"; // Insert a new row in the table for each person returned while($row = mysql_fetch_array($qry_result)){ $display_string .= "&lt;tr&gt;"; $display_string .= "&lt;td&gt;$row[name]&lt;/td&gt;"; $display_string .= "&lt;td&gt;$row[age]&lt;/td&gt;"; $display_string .= "&lt;td&gt;$row[sex]&lt;/td&gt;"; $display_string .= "&lt;td&gt;$row[wpm]&lt;/td&gt;"; $display_string .= "&lt;/tr&gt;"; } echo "Query: " . $query . "&lt;br /&gt;"; $display_string .= "&lt;/table&gt;"; echo $display_string; ?&gt; </code></pre>
    singulars
    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.
    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