[英]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.