使用ajax将多个变量发送到php文件?未定义的索引?

时间:2022-10-17 11:20:08

I am very new to Ajax and need to send 2 variables instead one 1 to a php file. I have 2 text input forms - after the value of the 2nd one is entered (the 2nd input is only shown if the first is entered) I need to send both the input from the FIRST INPUT along with the SECOND INPUT to a php file.

我是Ajax的新手,需要将2个变量而不是1个1发送到php文件。我有2个文本输入表单 - 在输入第二个值后(第二个输入仅在输入第一个时显示)我需要将FIRST INPUT的输入和SECOND INPUT一起发送到php文件。

Here are my form/inputs:

这是我的表格/输入:

echo '<form class="changePassForm" action="" method="post" enctype="multipart/form-data">';
echo '<input class = "passwordText" type="password" placeholder="Change Password" name="passwordText">';
echo '<input class = "oldPass" type="password" placeholder="Enter Old Password" name="oldPass">';
echo '</form>';

The javascript that currently send the variable containing input from the SECOND input only:

当前仅发送包含来自SECOND输入的输入的变量的javascript:

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
      var pass = $(this).val();

    $(".oldPass").keydown(function(event){
    if(event.keyCode == 13){
      var oldPass = $(this).val();
        //var pass = document.getElementById("p2");
    $.ajax({
        url: "../php/passwordchange2.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: 'oldPass=' + oldPass+'passwordText=' + pass, // data sent to php file
        //data: {pass:"passwordText",oldPass:"oldPass"}
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
            //$('.passwordText').slideUp(500)

        }});    
     console.log("WORKS now!!");   
    }
});
     //console.log("WORKS!!");   
    }
});

And my php which should echo both. As it is, only pass is echoed:

而我的php应该回应两者。实际上,只有通过回应:

session_start();
include("../php/Session.class.php");
$sess = new Session();
$sess->Init();
$cookie = isset($_COOKIE["session"]); 
if($cookie) 
{
$cookie = $_COOKIE["session"];
$account = $sess->Verify($cookie);
}

$pass1=$_POST['passwordText']; //name of input
    echo $pass1;    

$pass=$_POST['oldPass']; //name of input
    echo $pass;

I am getting the error Undefined index: passwordText How can I fix this?

我收到错误未定义索引:passwordText如何解决这个问题?

2 个解决方案

#1


0  

You are just missing "&" in your code:

您在代码中缺少“&”:

data: 'oldPass=' + oldPass+'&passwordText=' + pass, // data sent to php file

数据:'oldPass ='+ oldPass +'&passwordText ='+ pass,//数据发送到php文件

OR

data:{"oldPass":oldPass,"passwordText":pass}

#2


0  

way of sending ajax request with multiple data

用多个数据发送ajax请求的方法

$.ajax({
        method: "POST",
        url: /post_url,
        dataType: "json",
        data: {var1 : data1,
               var2 : data2,
               var3 : data3
              },
        beforeSend: function () {
         //do something
                  },
        error: function () {
          // do something                         
                  },
        success: function (r) {
                 //do something

                      }
        });

your javascript logic is not clear, you should modify it like this and define varibles globally.

你的javascript逻辑不清楚,你应该像这样修改它并全局定义varibles。

var pass=''; var oldPass='';

var pass =''; var oldPass ='';

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
       pass = $(this).val();
    }
});

$(".oldPass").keydown(function(event){
if(event.keyCode == 13){
  oldPass = $(this).val();
    $.ajax({
        url: "../php/passwordchange2.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: {pass:pass,oldPass:oldPass}// data sent to php file
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }
    });    
 console.log("WORKS now!!");   
}
});

#1


0  

You are just missing "&" in your code:

您在代码中缺少“&”:

data: 'oldPass=' + oldPass+'&passwordText=' + pass, // data sent to php file

数据:'oldPass ='+ oldPass +'&passwordText ='+ pass,//数据发送到php文件

OR

data:{"oldPass":oldPass,"passwordText":pass}

#2


0  

way of sending ajax request with multiple data

用多个数据发送ajax请求的方法

$.ajax({
        method: "POST",
        url: /post_url,
        dataType: "json",
        data: {var1 : data1,
               var2 : data2,
               var3 : data3
              },
        beforeSend: function () {
         //do something
                  },
        error: function () {
          // do something                         
                  },
        success: function (r) {
                 //do something

                      }
        });

your javascript logic is not clear, you should modify it like this and define varibles globally.

你的javascript逻辑不清楚,你应该像这样修改它并全局定义varibles。

var pass=''; var oldPass='';

var pass =''; var oldPass ='';

$(".passwordText").keydown(function(event){
    if(event.keyCode == 13){
       pass = $(this).val();
    }
});

$(".oldPass").keydown(function(event){
if(event.keyCode == 13){
  oldPass = $(this).val();
    $.ajax({
        url: "../php/passwordchange2.php", // Url to which the request is send
        type: "POST",             // Type of request to be send, called as method
        data: {pass:pass,oldPass:oldPass}// data sent to php file
        success: function(data)   // A function to be called if request succeeds
        {
            console.log(data);
        }
    });    
 console.log("WORKS now!!");   
}
});