duanshan3331 2012-05-12 17:10
浏览 59
已采纳

如何在成功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!!

  • 写回答

2条回答 默认 最新

  • douguwo2275 2012-05-12 17:23
    关注
    // 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.');
      }
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存