我正在使用 Jquery、Ajax 和 PHP 来尝试发送要写入 mysql 数据库的变量。正在发出 Ajax 请求,但 php.ini 未获取该变量。我不知道为什么会这样。
使用 firebug 和 console.log() 我可以看到已经对 write_results.php 进行了 POST
如果我检查它说的响应
注意:未定义的索引:第2行E:\write_results.php中的测试分数
这是我的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());
}
?>
这是我的ajax脚本
<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>
我的问题
- 为什么 $testscore 没有从 ajax 脚本接收值?
- 我怎样才能解决这个问题?