我遇到一个问题,即Ajax不提交给我的PHP处理表单。当我直接从表单提交到PHP文件时,它可以完成预期的工作,并且效果很好,但是当我尝试通过ajax提交时(我不希望在处理过程中进行任何加载),对于我所有的人,总是说“未定义索引:”变量等。
这是一种基本形式,因此不确定我在做什么错。在这方面需要另一双眼睛。我尝试了不同的发布方法,尝试在JS上添加/删除数据类型和数据等。有趣的是,我确实在PHP上收到了错误消息,该错误消息反馈到“ #formStatusMessage”,因此它从后到前进行通信,但它没有从头到尾发送数据。
我删除了所有无关紧要的代码,以便于阅读。非常感谢您的协助。
下面是我的代码:
HTML:
<form id="mainUserRegFormActual" method="post" >
<input type="text" id="uFname" name="uFname" placeholder="first name" value="ozarks">
<input type="text" id="uLname" name="uLname" placeholder="last name" value="sucks Balls">
<input type="text" id="uEmail" name="uEmail" placeholder="email" value="neverWatchingAgain@ever.com">
<input type="submit">
</form>
</div>
JS:
$("#mainUserRegFormActual").on("submit", function(evt){
evt.preventDefault();
var formProcURL = "proc.php";
var dataStringForStream = $("#mainUserRegFormActual").serialize();
$.ajax({
url : formProcURL,
type: "POST",
contentType: "application/json",
dataType: 'json',
data: dataStringForStream,
}).done(function(dataPassResponse){
$("#formStatusMssgs").text("passed data send --> " + dataPassResponse);
}).fail(function(dataFailResponse){
$("#formStatusMssgs").text("failed data send --> " + dataFailResponse.responseText);
});
});
PHP:
define('DB_NAME', 'testDB');
/** MySQL database username */
define('DB_USER', 'unameHere');
/** MySQL database password */
define('DB_PASSWORD', 'passHere');
/** MySQL hostname */
define('DB_HOST', 'localhost');
$c2d = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) or die("Failed to connect to database" . mysqli_error($c2d));
// =============================================== ][ user info from form ]
$uFnameClean=mysqli_real_escape_string($c2d, $_POST["uFname"]);
$uLnameClean=mysqli_real_escape_string($c2d, $_POST["uLname"]);
$uEmailClean=mysqli_real_escape_string($c2d, $_POST["uEmail"]);
// =============================================== ][ db query ]
$insertData = "INSERT INTO testDB (first_name, last_name, email_addy) VALUES ('$uFnameClean', '$uLnameClean', '$uEmailClean')";
// =============================================== ][ validation ]
if(empty($uFnameClean) || empty($uLnameClean) || empty($uEmailClean)){
http_response_code(400);
$userMessage = "Sorry, there was an error processing your form because you forgot to enter in either your First Name, Last Name, or your Email Address. " ;
echo $userMessage;
die();
} elseif(!filter_var($uEmailClean, FILTER_VALIDATE_EMAIL)){
http_response_code(400);
$userMessage = "Sorry, there was an error processing your form because the email you entered is incorrect. " ;
echo $userMessage;
die();
} elseif(preg_match('#[0-9]#',$uFnameClean) || preg_match('#[0-9]#',$uLnameClean)) {
echo "Unfortunately there was a problem saving your data. Your name has numbers";
die();
} else {
if($c2d){
if(mysqli_query($c2d, $insertData)){
http_response_code(200);
setcookie("userRegistered", true);
echo "Your data has been saved!";
}else{
http_response_code(400);
echo "Unfortunately there was a problem saving your data. Please try again later";
}
}else{
http_response_code(400);
echo "no connection";
}
}
答案 0 :(得分:3)
您的Ajax请求中的内容类型错误,当您输入的内容类型不是application/json
时。
从ajax请求中删除内容类型参数
$.ajax({
url : formProcURL,
type: "POST",
//dataType: 'json',
data: dataStringForStream,
}).done(function(dataPassResponse){
$("#formStatusMssgs").text("passed data send --> " + dataPassResponse);
}).fail(function(dataFailResponse){
$("#formStatusMssgs").text("failed data send --> " + dataFailResponse.responseText);
});
$。ajax的默认内容类型是application/x-www-form-urlencoded
,这是您从.serialize()
获得的内容,它是用来填充$_POST
超全局变量的内容
由于您使用纯文本而不是json进行响应,因此dataType: 'json',
也应删除。
答案 1 :(得分:2)
我相信问题可能出在您用来发送数据的 contentType 上。在您提供的表单数据上使用 serialize()时,得到以下输出:
uFname = ozarks&uLname = sucks%20Balls&uEmail = neverWatchingAgain%40ever.com
此数据未格式化为JSON。如果删除 contentType 参数,则数据应正确发送。