简体   繁体   English

如何使用GET发送变量而无需重新加载页面

[英]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. 我有一个php页面,可从mysql数据库中加载一些变量。 Then I want to send these variables using GET to another page without opening the page where the variables are being send to. 然后,我想使用GET将这些变量发送到另一个页面,而无需打开要将变量发送到的页面。

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. $test$test1是我想发送到另一页的php变量。 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 我正在使用以下jquery版本: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. 刚分配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.

相关问题 发送GET变量而无需使用javascript重新加载页面 - Send GET variables without reloading the page with javascript 如何在不重新加载页面的情况下将信息发送到数据库? - How to send information to database without reloading page? 如何在不重新加载页面的情况下更新jsp页面上的变量? - How can I update variables on jsp page without reloading page? 如何在不重新加载页面的情况下将值从 controller 发送到 ejs:Nodejs - How to send value from controller to ejs without reloading page : Nodejs 如何使用Ajax发送数据而无需重新加载页面? (Node.js) - How to send data with ajax, without reloading page? (Nodejs) 如何在不重新加载页面的情况下使用AJAX将表单值发送到php? - how to send form values to php using AJAX without reloading page? 通过cURL发送数据而无需重新加载页面 - Send data via cURL without reloading page 如何在不重新加载页面的情况下将新值添加到Cookie? - How to get the new value added to a cookie without reloading the page? 如何在不重新加载的情况下从jsp页面获取响应? - How to get response from jsp page without reloading? IndexedDB,如何在不重新加载页面的情况下获取更新的数据? - IndexedDB, How to get updated data without reloading the page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM