简体   繁体   中英

jQuery - send same variables to two different PHP pages

In my site I'd like to send two variables to two different PHP pages.

The fact is that the two variables are being successfully sent to the first page only, and not to both pages.

The code I'm using is:

auth.js page

$(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;

});

and then, I'm getting the data in both pages (auth3.php and landmarks.php) using:

$surname=mysql_real_escape_string(htmlspecialchars(trim($_POST['surname']))); $password=mysql_real_escape_string(htmlspecialchars(trim($_POST['password'])));

Once again, the unique page that gets the data from auth.js is auth3.php

Could you try changing your code in this way: on the success function for the first ajax call the second function. This because ajax is asynchronous.

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

};

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