I m using Jquery, Ajax and PHP to try and send a variable to be written in a mysql database. The Ajax request is being made but the variable is not being picked up by the php. I don t know why this is the case.
Using firebug and console.log() I can see that a POST has been made to write_results.php
If I check the Response it says
Notice : Undefined index: testscore in E:write_results.php on line 2
Here is my PHP
<?php
$testscore=$_POST[ testscore ]; //get testscore from Ajax
include DB.php ;
$con = mysql_connect($host,$user,$pass);
$dbs = mysql_select_db($databaseName, $con);
if (isset($_POST[ testscore ])) {
$addClient = "INSERT INTO variables (`id` ,`name`) VALUES (NULL, $testscore )";
mysql_query($addClient) or die(mysql_error());
}
?>
Here is my ajax script
<script type="text/javascript">
$(document).ready(function() {
testscore ="tryagain"; //testvalue to enter into the mysql database
$.ajax({
type: "POST",
url: "write_results.php",
data: testscore,
success: function(){
$( #box2 ).html("success");
}
})
});
</script>
My questions
- Why isn t $testscore receiving a value from the ajax script?
- How can I fix this?