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 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)