我无法理解为什么在PHP的$ _POST部分中说未定义的变量num_guess和user_name。
这是 HTML部分:
<div id="user">
<img src="Design/Chest12.gif" alt="Chest" name="Chest">
<div id="msgbox"><span id="msg"></span></div>
<form id="score_form">
<div class="formtags">
<ul>
<li><label for="user">Name: </label></li>
<li><label for="num_guess">Guesses: </label></li>
</ul>
</div>
<div class="forms">
<ul>
<li><input type="text" name="user_name" id="user_name"/></li>
<li><input type="text" name="num_guess" id="num_guess"/></li>
</ul>
</div>
<div class="button">
<ul>
<li><input type="submit" id="submit" value="Submit"/></li>
<li><input type="submit" id="cancel" value="Play Again"/></li>
</ul>
</div>
</form>
这是 AJAX部分:
$(document).ready(function() {
$("#user #msgbox").hide();
$("#user .button ul li #submit").click( function(){
$.request("utilities.php",{
'user_name':$("#user_name").val(),
'num_guess':$("#num_guess").val(),
},
function(response){
$("#msgbox #msg").html("Score Submitted");
$("#msgbox").slideUp().slideDown("slow");
});
return false;
});
$("#user #msgbox").click(function(){
$(this).slideUp("slow");
});
});
这是 PHP部分:
function insertScore($name,$num_guess){
opendb();
$query = mysqli_query($GLOBALS['dbconn'],"INSERT INTO game VALUES (0,'$name',$num_guess)");
closedb();
}
$name = $_POST['user_name'];
$num_guess =(int) $_POST['num_guess'];
insertScore($name,$num_guess);
?>
答案 0 :(得分:0)
您可以使用:
if(isset($_POST['user_name'])){
$name = $_POST['user_name'];
}
if(isset($_POST['num_guess'])){
$num_guess =(int) $_POST['num_guess'];
}
//now
if(isset($name) && isset($num_guess)){
insertScore($name,$num_guess);
}
你得到的通知是正常的,如果发现任何未定义的变量,PHP将触发该通知,解决这个问题的唯一方法是在为其分配nay变量之前检查$ _POST。