Javascript Ajax不提交给PHP
我遇到了一个问题,Ajax没有提交到我的PHP处理表单。当我直接从表单提交到PHP文件时,它做了它应该做的事情,效果很好,但当我尝试通过ajax提交时(我不希望在处理过程中加载),它总是对我的所有变量等说“未定义索引” 这是一种基本形式,所以我不确定我做错了什么。需要一双不同的眼睛。我尝试过不同的发布方法,我尝试过在JS上添加/删除数据类型和数据等。有趣的是,我确实收到了反馈到“#formStatusMessage”的PHP上的错误消息,因此它是从前到后通信的,但它不是从前到后发送数据 为了便于阅读,我删去了所有无关紧要的代码。非常感谢您的帮助 下面是我的代码: HTML: PHP:Javascript Ajax不提交给PHP,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我遇到了一个问题,Ajax没有提交到我的PHP处理表单。当我直接从表单提交到PHP文件时,它做了它应该做的事情,效果很好,但当我尝试通过ajax提交时(我不希望在处理过程中加载),它总是对我的所有变量等说“未定义索引” 这是一种基本形式,所以我不确定我做错了什么。需要一双不同的眼睛。我尝试过不同的发布方法,我尝试过在JS上添加/删除数据类型和数据等。有趣的是,我确实收到了反馈到“#formStatusMessage”的PHP上的错误消息,因此它是从前到后通信的,但它不是从前到后发送数据 为了便于
ajax请求中的内容类型错误,如果不是,则将其作为
application/json
。从ajax请求中删除content-type参数
$.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()
获得的内容,用于填充$\u POST
超级全局
由于您使用的是纯文本而不是json响应,
数据类型:“json”,因此也应删除。您的ajax请求中的内容类型错误,如果不正确,则将其作为应用程序/json
。
从ajax请求中删除content-type参数
$.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()
获得的内容,用于填充$\u POST
超级全局
由于您是用纯文本而不是json响应,数据类型:“json”,因此也应该删除。我认为问题可能在于您用于发送数据的contentType。当我对您提供的表单数据使用serialize()时,会得到以下输出:
uFname=ozarks&uLname=sucks%20Balls&uEmail=neverWatchingAgain%40ever.com
此数据未格式化为JSON。如果删除contentType参数,您的数据应能正确发送。我认为问题可能在于您用于发送数据的contentType。当我对您提供的表单数据使用serialize()时,会得到以下输出:
uFname=ozarks&uLname=sucks%20Balls&uEmail=neverWatchingAgain%40ever.com
此数据未格式化为JSON。如果删除contentType参数,数据应能正确发送。dataType:'json',
也可以省略,因为响应是原始文本,但不是jsonned数据dataType:'json',
也可以省略,因为响应是原始文本,而不是jsonned数据
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";
}
}
$.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);
});