Note that there are some explanatory texts on larger screens.

plurals
  1. POPhp-javascript code working only once
    primarykey
    data
    text
    <p>I have simple code that inserts values to database without page refresh using javascript. The problem is that when I use "onchange" atrribute in method to call the function the code works fine and value is inserted but when i remove "onchange" form and use a Button "onclick" attribute to call same method it works on once and then stops working.</p> <p>The code of my <strong>comment.html</strong> file is</p> <pre><code> &lt;html&gt; &lt;head&gt; &lt;script&gt; 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","getuser.php?q="+str,true); xmlhttp.send(); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form method="GET"&gt; &lt;input type="text" name="q"&gt; &lt;button method="GET" onclick="showUser(q.value)"&gt;Submit&lt;/button&gt; &lt;/form&gt; &lt;br&gt; &lt;div id="txtHint"&gt;&lt;b&gt;Person info will be listed here.&lt;/b&gt;&lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And the code of my <strong>getuser.php</strong> file is:</p> <pre><code> &lt;?php $q = intval($_GET['q']); $con = mysqli_connect('localhost','root','','login'); if (!$con) { die('Could not connect: ' . mysqli_error($con)); } mysqli_select_db($con,"ajax_demo"); $sql2= "INSERT INTO `users` ( `FirstName`) VALUES( '{$_GET['q']}') "; $result = mysqli_query($con,$sql2); while($row = $result) { echo "&lt;tr&gt;"; echo "&lt;td&gt;" . $row['FirstName'] . "&lt;/td&gt;"; echo "&lt;/tr&gt;"; } echo "&lt;/table&gt;"; mysqli_close($con); ?&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.
 

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