[英]jquery-ajax: pass values from a textfield to a php variable (same a file)
[英]How to pass values from AJAX and JQuery to PHP in the same file
我目前有以下腳本。 我想通過使用AJAX將值從javascript傳遞給php。 我的代碼出了什么問題?
<script type=" text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$.post("index.php",{host:document.referrer},function(data){});
});
</script>
<?php
$dataString=$_POST['host'];
echo $dataString;
?>
由於首先執行PHP,因此您永遠不會從AJAX請求中看到echo $dataString
。 此代碼會將您的請求發布到服務器,但您永遠不會看到結果。
現在正是這里發生的事情:
從文檔中你可以看到:
$.post('index.php', function(data) {
$('.result').html(data);
});
該函數中的data
將返回echo $dataString;
腳本輸出。
此外,您的帖子配置不正確。 你需要把data:
放在{host:document.referrer}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.