1

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.

The code of my comment.html file is

 <html>
<head>
<script>
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 && xmlhttp.status==200)
    {
    document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<form method="GET">
<input type="text" name="q">
<button method="GET" onclick="showUser(q.value)">Submit</button>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here.</b></div>

</body>
</html>

And the code of my getuser.php file is:

  <?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 "<tr>";
  echo "<td>" . $row['FirstName'] . "</td>";
  echo "</tr>";
  }
echo "</table>";

mysqli_close($con);
?>
6
  • You should keep your mysqli connection on a separate secure page, using new mysqli(/*args*/), and use the Object Oriented PHP style. It makes life easier. Commented Sep 20, 2013 at 23:45
  • If you put an alert in showUser() does it fire? Commented Sep 20, 2013 at 23:48
  • Yes alert is getting fired Commented Sep 20, 2013 at 23:51
  • Does str == ''? If your input is empty or the number 0, your function returns before executing AJAX. Commented Sep 20, 2013 at 23:51
  • 1
    Why are people so insisting on using both a form and ajax at the same time? Commented Sep 20, 2013 at 23:52

2 Answers 2

2

code is correct except the button that you've created.

use: <input type="button" onclick="showUser(q.value)" value="Submit">

instead of: <button method="GET" onclick="showUser(q.value)">Submit</button>

What you've done is actually submitting the form. So, the onclick event is being overridden.

Sign up to request clarification or add additional context in comments.

4 Comments

No, even if you remove method="GET" it is the same. You have to remove <form> ..</form>
Its working but dont know why now nothing is being appended in URL
On removing the method="GET" will not solve the problem. You can even try that thing. The <form> thinks <button> as a submit parameter rather than a simple button.
Prince: Nothing is being appended in URL because you're calling the PHP file through Ajax. Actually, it is being appended but that's not visible to you.. ;)
0

@Lavneet is in right direction, but does not solve the problem.

If you insist of that setup you must return false in the onclick :

<button onclick="showUser(q.value);return false;">Submit</button>

by that, the form is not re-submitted after showUser().

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.