在我的网站中,我想将两个变量发送到两个不同的PHP页面。
事实是这两个变量只是成功发送到第一页,而不是两个页面。
我使用的代码是:
auth.js页面
$(document).ready(function(){
var url="auth3.php?callback=?";
var url2="landmarks.php?callback=?";
//SENDING VARIABLES TO THE FIRST PAGE
$("#login").click(function(){
var surname=$("#surname").val();
var password=$("#password").val();
var dataString="surname="+surname+"&password="+password+"&login=";
if($.trim(surname).length>0 & $.trim(password).length>0)
{
$.ajax({
type: "POST",
url: url,
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){ $("#login").html('Connecting...');},
success: function(data){
if(data=="success")
{
localStorage.login="true";
localStorage.surname=surname;
window.location.href = "index.html";
}
else if(data="failed")
{
$("#login").html('Login');
}
}
});
}return false;
});
//SENDING VARIABLES TO THE SECOND PAGE
$("#login").click(function(){
var surname2=$("#surname").val();
var password2=$("#password").val();
var dataString2="surname="+surname2+"&password="+password2+"&login=";
if($.trim(surname2).length>0 & $.trim(password2).length>0)
{
$.ajax({
type: "POST",
url: url2,
data: dataString2,
crossDomain: true,
cache: false
});
}return false;
});
然后,我使用以下方式获取两个页面中的数据(auth3.php和landmarks.php):
$姓= mysql_real_escape_string(用htmlspecialchars(修剪($ _ POST ['姓']))); $密码= mysql_real_escape_string(用htmlspecialchars(修剪($ _ POST ['密码'])));
再一次,从auth.js获取数据的唯一页面是auth3.php
答案 0 :(得分:0)
你能尝试用这种方式改变你的代码:第一个ajax的成功函数调用第二个函数。这是因为ajax是异步的。
$("#login").click(function(){
var surname=$("#surname").val();
var password=$("#password").val();
var dataString="surname="+surname+"&password="+password+"&login=";
if($.trim(surname).length>0 & $.trim(password).length>0)
{
$.ajax({
type: "POST",
url: url,
data: dataString,
crossDomain: true,
cache: false,
beforeSend: function(){ $("#login").html('Connecting...');},
success: function(data){
if(data=="success")
{
localStorage.login="true";
localStorage.surname=surname;
window.location.href = "index.html";
}
else if(data="failed")
{
$("#login").html('Login');
}
newFunction();
}
});
}return false;
});
//SENDING VARIABLES TO THE SECOND PAGE
function newFunction() {
var surname2=$("#surname").val();
var password2=$("#password").val();
var dataString2="surname="+surname2+"&password="+password2+"&login=";
if($.trim(surname2).length>0 & $.trim(password2).length>0)
{
$.ajax({
type: "POST",
url: url2,
data: dataString2,
crossDomain: true,
cache: false
});
}return false;
};