我是Ajax的新手,需要将2个变量而不是1个1发送到php文件。 我有2个文本输入表单 - 在输入第二个值后(第二个输入仅在输入第一个时显示)我需要将FIRST INPUT的输入和SECOND INPUT一起发送到php文件。
以下是我的表格/输入:
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>';
当前仅发送包含来自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!!");
}
});
我的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;
我收到错误Undefined index: passwordText
我该如何解决这个问题?
答案 0 :(得分:0)
你只是缺少“&amp;”在你的代码中:
data: 'oldPass=' + oldPass+'&passwordText=' + pass, // data sent to php file
OR
data:{"oldPass":oldPass,"passwordText":pass}
答案 1 :(得分:0)
使用多个数据发送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
}
});
你的javascript逻辑不清楚,你应该像这样修改它并全局定义varibles。
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!!");
}
});