我不知道为什么我得到这个奇怪的错误!
PHP注意:未定义的索引:refId在/var/www/echo. PHP第5行
我得到控制台输出,但不能回显refId
。我做错什么了吗?
<?php
$rollUrl = 34;
$refId = $_POST['refId'];
echo $refId;
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response){
console.log('Getting response');
}
});
</script>
请参阅下面代码中的注释:
<?php
$rollUrl = 34;
//Only try to process POST if there is something posted *and* refId exists
if (count($_POST) > 0 && isset($_POST['refId'])) {
$refId = $_POST['refId'];
echo $refId;
//Exit after echoing out the refId so that the HTML below does not also get returned.
exit();
}
?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$.ajax({
url:'echo.php',
type: 'POST',
data: { 'refId': "<?php echo $rollUrl ?>" },
success: function(response) {
//Updated log to show the actual response received.
console.log('Getting response of "' + response + '"');
}
});
</script>
当我进行测试时,没有抛出任何错误,也没有执行Ajax。
发生这种情况是因为没有设置变量。使用isset
<?php
$rollUrl=34;
if(isset($_POST['refId'])) {
$refId=$_POST['refId'];
echo $refId;
}
?>
:您应该将refId
作为名称属性分配给任何输入字段,以恢复用户的输入。
<input type="text" name="refId" />