AJAX 请求不发布表单数据
ajax request not posting form data
我有一个问题,在我的登录表单中输入的值(电子邮件和密码)没有被解析到我的服务器php脚本。
我的 ajax 请求:
function signin(){
var loginEmail = gebi("loginEmail").value;
var loginPass = gebi("loginPass").value;
if(loginEmail == "" || loginPass == ""){
gebi("loginEmail").style.borderColor = "red";
gebi("loginPass").style.borderColor = "red";
} else {
gebi("signinBtn").style.display = "none";
//Declare ajax request variables
hr = new XMLHttpRequest();
url = "main.php";
vars = "email="+loginEmail+"&pass="+loginPass;
//Open the PHP file that is receiving the request
hr.open("POST", url, true);
//Set content type header info for sending url ecncoded variable in the request
hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//Trim string data before sending it
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^'s+|'s+$/g, '');
};
}
//Access the onreadystatechange event for the XMLHttpRequest
hr.onreadystatechange = function() {
if (hr.readyState == 4 && hr.status == 200) {
var responseText = hr.responseText.trim();
if (responseText != "signin_failed") {
console.log(responseText);
} else {
console.log(responseText);
gebi("signinBtn").style.display = "block";
gebi("loginEmail").style.borderColor = "red";
gebi("loginPass").style.borderColor = "red";
}
}
}
//Send the data to the PHP file for processing and wait for responseText
hr.send(vars);
}
}
当 PHP 脚本使用以下代码返回值时:
if (isset($_POST["email"])) {
echo 'email = '+$_POST["email"];
}
记录到控制台的返回值为"0",即使表单字段中存在数据也是如此。
出了什么问题?
问题出在您的PHP
脚本中。如果您尝试在 PHP 中连接字符串,请使用点 .
。 +
用于连接JavaScript等语言中的字符串。
echo 'email = ' . $_POST["email"];
相关文章:
- 如何从pdftron webviewer获取表单数据
- JS验证ajax返回的html中的表单数据
- 在不同的文件中使用Javascript获取表单数据
- 使用Javascript读取Webstorage表单数据
- Uploadify-随机表单数据's已返回上载表单
- 如何在使用ajax提交的codeigniter中使用form_validation验证表单数据
- 在post-expressjs之后持久化表单数据
- 需要一个用于AJAX的Javascript库,其中包含多部分/表单数据
- jQuery发布并获取表单数据
- Ajax表单数据phpPOST不工作
- 如何将整个模型集附加到表单数据中,并在MVC中获得它
- 如何从javascript访问表单数据
- 如何从Angular Payments获取表单数据
- 通过javascript获取pdf文件的表单数据
- 有没有一种方法可以在jQuery.serialize()之前获取表单数据,或者与jQuery.sserialize(()
- 在 Angular2 中构建多部分/表单数据 POST 请求并验证输入类型文件
- 无法读取未定义的属性“用户名” - 通过电子邮件在 Node.js 中发送表单数据
- 使用 Ajax 获取表单数据
- 我无法从引导模式中清除表单数据
- 如何从 django 模板语言获取表单数据