[英]How do I save my data from my POST request, as a variable, to use in my GET request?
[英]How do I get my jquery post request to work with the variable stored from a function on a click?
我一直在撞牆,試圖在用戶單擊按鈕后立即將游戲名稱傳遞給我的php函數。 這個想法是用戶單擊一個按鈕,該按鈕具有其視頻游戲名稱的值,然后php腳本檢查該游戲的排名並將該排名返回給html。 我以前曾要求發布請求,即使在這里,當我手動將變量名設置為游戲之一來測試它時,它也可以工作。 請幫忙!
<script>
$(document).ready(function(){
//global n variable
n = $();
$('#target').click(function(){
//set value of n to the game's name
n = $(this).val();
});
//then send the name as term to my php function to deal with it and return
//the ranking
$.post('getTotal.php', {term: n}, function(data){
//been trying to see if anything comes through for debugging purposes
alert(data);
//commented this out for now, just puts it in the div I left open
//$('#total').html(data);
});
});
</script>
您應該將$.post
放入您的點擊處理程序中,以便它僅在您實際單擊按鈕時才會運行...現在您的代碼設置了一個n
變量,其值是一個空的jQuery對象(為什么?)。 然后,它在按鈕上附加一個單擊處理程序。 然后,它運行$.post
請求n
仍然是一個空的jQuery對象。 單擊按鈕的時間要晚得多...
另外,應避免使用全局變量。 聲明變量時應使用var
關鍵字。
僅當用戶單擊按鈕時。 在點擊處理程序中,獲取值並執行http發布
$ ajax或$ POST
例如-
$(document).ready(function() {
$('#target').click(function()
$.ajax({
type: "POST",
url: "url...",
data: "n="+nVal+",
success: function(html){
alert( "Submitted");
}
});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.