如何在成功jquery.ajax()之后将变量传递给另一个php文件?

如何在成功jquery.ajax()之后将变量传递给另一个php文件?

问题描述:

I've a index.php for login users, and when press submit the following process occurs ...

1.- The user enters a number and password

2.- jQuery.ajax method is used () to connect to the file doLogin.php to process the data and if correct, print "echo 'success'" and make these variables sessión: nControl , name, lastname, and typeUser

3.- Then in the method jQuery.ajax () takes "success" and makes a call to dashboard.php thus: "document.location.href = 'dashboard.php'"

Basic structure: Index.php (for login) --> functions.js (process inputs of index.php with jQuer.ajax()) --> dashboard.php(i want receive data from index.php to displa details user)

So the question that arises is: That method would be best for you, after success in jQuery.ajax method () that sends data to the file dashboard.php? Because such data as name, lastname, typeUser and nControl, I want to print and a div for the user to see the details of your login.

Maybe I can be in JSON format but not how. I hope I have explained!!

// take username and password on button click

$('#submit').on('click', function(e) {
  e.preventDefault();
  var uname = $.trim($('input[name="username"]').val()),
    password = $.trim($('input[name="password"]').val());

  if(uname.length && password.length){ // checking that username and password not empty
    $.ajax({
      url : 'doLogin.php',
      data: {username: uname, password: password},
      dataType: 'json', // if you get response from server as json
      success: function(response) {

        // receive the response as json object

        if(response.status == 'success') {

           alert('You have been successfully authenticated');

           /**
             * assume that you get other 
             * data with response
             */ 

            var name = response.name,
                lastname = response.lastname,
                typeUser = response.typeUser,
                nControl = response.nControl;

           // make navigation to dashboard.php with data you've got using GET method
           // you may have need to location url as you need
           window.location = 'dashboard.php?name=' + name + '&lastname=' + lastname + '&typeUser=' + typeUser + '&nControl=' + nControl;

        } else {
           alert('Error is authentication..');
        }
      }
    });
  } else {
     // make alert if username or password not provided by user
     alert('Please enter password and username both.');
  }
});

Didnt have time to test. I dont have php on test server. You'll need the jquery.form.js plugin for JQuery.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.form.js"></script>

<script type="text/javascript">
$('#loginbutton').click(function(){
$('#error').empty();
$('#loginform').ajaxForm(function(data, textStatus){
$('#error').append(data);
});
});
</script>

<div id="error"></div>

<form id="loginform" method="post" action="login.php">
<input type="text" name="username" />
<input type="password" name="password" />
<button id="loginbutton">Log In</button>
</form>

On login.php page check against database for correct username and password (my php is a little rusty)

$connect = mysql_connect("$server","$username","$password") or die("not connecting");
mysql_select_db("users") or die("no db :'(");
$query = mysql_query("SELECT * FROM $tablename WHERE username='$user' AND password='$password'");

$numrows = mysql_num_rows($query);

if ($numrows!=0)
{
//if row returned send them to home page
<script type="text/javascript">
window.location.replace("dashboard.php");
</script>
}
else
//If not correct login then the error div reports this message
echo "Incorrect Login Information";
}