簡體   English   中英

如何使用GET發送變量而無需重新加載頁面

[英]How to send variables with GET without reloading page

我想要完成的是以下內容。 我有一個php頁面,可從mysql數據庫中加載一些變量。 然后,我想使用GET將這些變量發送到另一個頁面,而無需打開要將變量發送到的頁面。

剛開始,我真的不知道該怎么做,直到遇到以下問題:

            $( document ).ready(function()
            {
                $.ajax({
                  url: 'the_url',
                  type: 'GET',
                  data: {Gender:Male,DateOfBirth:1968-07-21},
                  success: function(data) {
                    //called when successful
                    alert("Succes");
                  },
                  error: function(e) {
                    //called when there is an error
                    console.log(e.message);
                    alert("Failed");
                  }
                }); 
            }

$test$test1是我想發送到另一頁的php變量。 但顯然我在做錯事。 甚至沒有觸發警報,因此我的語法很可能出了點問題。

我正在使用以下jquery版本:jquery-2.1.4.min.js

如果我問這個問題的方式有問題,請告訴我,並給我幫助以更新問題。

剛分配PHP變量JS變量,改變它的數據發送部分,也是沒有必要的'用於數據發送。

$( document ).ready(function()
{
    var test = '<?php echo $test; ?>';
    var test1 = '<?php echo $test1; ?>';
    $.ajax({
            url: 'the_url',
            type: 'POST',   //change type to POST rather than GET because this POST method of sending data
            data: {test:test,test1:test1},
            success: function(data) {
                 //called when successful
                 $('#ajaxphp-results').html(data);
                 alert("succes");
            },
            error: function(e) {
                 //called when there is an error
                 console.log(e.message);
                 alert("failed");
            }
    }); 
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM