私はJquery、Ajax、PHPを使用して、mysqlデータベースに書き込まれる変数を送信しようとしています。Ajaxリクエストが行われていますが、変数がphpによって取得されていません。なぜそうなのかわかりません。
firebugとconsole.log()を使用すると、write_results.phpに対してPOSTが行われたことがわかります。
応答を確認すると、
注意:未定義のインデックス:2行目のE:\write_results.phpのtestscore
これが私の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スクリプトから値を受け取らないのはなぜですか?
- どうすればこれを修正できますか?