当我从 JS 端得到结果时,该值变为 true。但我想将值填充到我的 PHP 文件中,所以我使用了 ajax。当我删除 PHP 中的 ifisset 函数时,出现“未定义索引”错误。
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.";
}
?>
哈士奇WWW