當我從 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."; } ?>
1 回答

哈士奇WWW
TA貢獻1799條經驗 獲得超6個贊
您正在發送一個值,但您沒有發送具有該值的密鑰。例如,如果值為123
則您只發送123
,沒有其他內容。ad
所以這不會找到它,因為發送的數據中沒有調用密鑰:
$_POST["ad"]
為值添加一個鍵:
data: { ad: ad }
或者甚至簡單地:
data: { ad }
- 1 回答
- 0 關注
- 85 瀏覽
添加回答
舉報
0/150
提交
取消