简体   繁体   中英

How to send variables with GET without reloading page

What I'm trying to accomplish is the following. I have a php page which loads some variables from an mysql db. Then I want to send these variables using GET to another page without opening the page where the variables are being send to.

At first I really didn't know how to do this until I came across the following:

            $( 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");
                  }
                }); 
            }

The $test and $test1 are php variables which I would like to send to the other page. But apparently i'm doing something wrong. Not even the alerts are being triggered so there is probaly something wrong with my syntax.

I'm using the following jquery version: jquery-2.1.4.min.js

If there is something wrong in the way I asked this question please let me know and give me the help to update the question.

Just assign PHP variables to JS variables and also change the data sending part as well there is no need of ' for data sending.

$( 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");
            }
    }); 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM