当我从JS端获得结果时,该值将变为true。但是我想将值填充到我的PHP文件中,所以我使用了ajax。当我删除PHP中的if-iset函数时,我得到了一个"未定义索引";错误
HTML侧
<form method="post" onsubmit="return false;" class="form-inline">
<input type="email" name="email" id="subscriber_email" placeholder="Your E-mail" >
<button type="submit" id="subscribe_newsletter" class="btn newsbox-btn w-
100"onclick="gonder()">Subscribe</button>
</form>
<div id="subscribe_message"></div>
<p id="succes" class="mt-30"></p>
jS侧
<script type="text/javascript">
function gonder(){
var ad=$("input[name='email']").val();
$.ajax({
type:"POST",
url:"myphpfile.php",
data:ad, // I also tried {'sendingData':ad} but it doesn't work.
success:function(sonuc){
$("#subscribe_message").html(sonuc);
$("#succes").html(sonuc);
}
})
}
</script>
和PHP端
<?php
if(isset($_POST["ad"])){
$x=$_POST["ad"];
echo $x;
}
else{
echo "There is nothing coming from the script.";
}
?>
您正在发送一个值,但没有发送带有该值的键。例如,如果值是123
,那么您只发送123
,而不发送其他内容。因此,它找不到它,因为在发送的数据中没有名为ad
的密钥:
$_POST["ad"]
为值添加一个密钥:
data: { ad: ad }
甚至简单地说:
data: { ad }