Question: There is a button in my html file upon click I need two things to be done. (Insert data to a database and reload the page) So I call a javascript function in the onclick event.

This is the function inside the javascript file:

```
function grandFinale()
{
     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","insert.php?q=",true);
xmlhttp.send();
document.location.reload();

}

```

This is the insert.php file which is mentioned above

```
<?php
    $con = mysql_connect("localhost","uname","password");
if (!$con)
  {
   die('Could not connect: ' . mysql_error());
  }

mysql_select_db("SnakeDB", $con);

$sql="INSERT INTO masterTable (Name, Score) VALUES ('$_GET[name]','$_POST[txtScore1]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

```

Here is the part of HTML file which contains the places I want to update to the database

```
<div class="modal-body">
 <p>You scored :: </p><p id="txtScore1"></p>
<input type="text" placeholder="Your Name" id="name"> </div>

```

The issue is the database is not updated!! Only the reloading happens. Please suggest me a solution

Answer: 
As fare as I can see didn't you send the parameters to the script?!

Append the name field also to the 

```
xmlhttp.open("GET","insert.php?q=&name=" + encodeURIComponent(document.getElementById('name').value),true);

```

The given code is not tested but should work this way.
The error in your javascript is, that you are not sending any values to your script via ajax.

```
xmlhttp.open("GET","insert.php?q=",true);

```

`q` ist just empty, `name` and `txtScore1` are not set.

Plus in your SQL-Statement you are mixing up GET and POST values.

$sql="INSERT INTO masterTable (Name, Score) VALUES ('$\_GET[name]','$\_POST[txtScore1]')";

Should be written as:

```
   $sql="INSERT INTO masterTable (Name, Score) VALUES ('{$_GET['name']}','{$_GET['txtScore1']}')";

```

Warning, you should always check GET or POST data before insering it to the database!

You tagged your Question with jQuery but you aint using it. You should. It would condense your javascript to just a few lines + reloading of the page is not needed!

See <http://api.jquery.com/jQuery.getJSON/>