Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to apply ajax on php code
    primarykey
    data
    text
    <p>I am trying to apply ajax to my php code. However, when I click on the button I can't get any response. This means the ajax function I declare is not being called <code>onclick</code>.</p> <pre><code>&lt;?php $s_name= $_POST["submit"]; mysql_connect("localhost","root","");//database connection mysql_select_db("itcompanylist"); $query = "SELECT s_id FROM states WHERE `state_name` = '$s_name'"; $result1 = mysql_query($query); $row = mysql_fetch_array($result1); $result2 = mysql_query("SELECT city_name FROM `city` WHERE s_id ='".$row['s_id']."'"); $i = 0; echo "&lt;form method='post' name='myForm'&gt;&lt;table border='1' &gt;&lt;tr&gt;"; while ($row = mysql_fetch_row($result2)){ echo '&lt;td&gt;&lt;input type="submit" name="submit" onclick="ajaxFunction()" value="'.$row['0'].'"&gt;&lt;/td&gt;'; if ($i++ == 2) { echo "&lt;/tr&gt;&lt;tr&gt;"; $i=0; } } echo "&lt;/tr&gt;&lt;/table&gt;&lt;/form&gt;"; echo "&lt;div id='ajaxDiv'&gt;Your result will display here&lt;/div&gt;"; ?&gt; </code></pre> <p>ajax code:</p> <pre><code>&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; } } } ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('ajaxDiv'); ajaxDisplay.value = ajaxRequest.responseText; } } var s1 = document.getElementById('submit').value; var queryString = "?submit=" + s1 ; ajaxRequest.open("GET", "" + queryString, true); ajaxRequest.send(null); } //--&gt; &lt;/script&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.
 

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