PHP发布请求未识别的索引错误
I'm trying to write a page to make a POST request to a php script and I feel like I've done it right, it's worked everywhere else so it seems but I keep getting a "unidentified error" and it won't work, how can I get this to work?
我正在尝试编写一个页面来对PHP脚本发出POST请求,我觉得我做得对,它在其他任何地方都有效,所以看起来似乎但我一直得到一个“未识别的错误”,它将无法正常工作,我怎么能让这个工作?
Javascript:
$(document).ready(function() {
$("#x").click(function() {
var email = $("email").val();
var pass = $("password").val();
var confirmPass = $("confirmPassword").val();
var name = $("name").val();
var question = $("question").val();
var answer = $("answer").val();
if(pass != confirmPass) {
alert("Passwords do not match!");
return;
}
var stuff = {email: email, pass: pass, name: name, question: question, answer: answer};
$.ajax({method: "POST", url: "addAccount.php", data: stuff, success: function(result) {
alert(result);
window.location.href = "../Dashboard";
}});
});
});
PHP:
<?php
$servername = "localhost";
$username = "root";
$password = "*********";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
$email = $_POST["email"];
$pass = $_POST["pass"];
$name = $_POST["name"];
$question = $_POST["question"];
$answer = $_POST["answer"];
$sql = "INSERT INTO accounts (accountEmail, accountPassword, accountName, accountQuestion, accountRecover) VALUES ('$email', '$pass', '$name', '$question', '$answer')";
$conn->close();
if(mysql_affected_rows() > 0) {
$response = "Account added successfully!";
}
else {
$response = "Couldn't add account!";
}
$pre = array("Response" => $response);
echo json_encode($pre);
?>
3 个解决方案
#1
0
You need to properly use jquery.
For example var email = $("email").val(); //IS WRONG
Should be (if you have input id="email") var email = $("#email").val();
If you have only name you can use var email = $("[name='email']").val();
你需要正确使用jquery。例如var email = $(“email”)。val(); //错误应该是(如果你有输入id =“email”)var email = $(“#email”)。val();如果您只有名字,可以使用var email = $(“[name ='email']”)。val();
A bit offtopic: If you are using form ajax submit consider jquery method serialize https://api.jquery.com/serialize/ for getting all form values (or some jquery ajaxform plugin).
有点offtopic:如果你正在使用表单ajax提交考虑jquery方法序列化https://api.jquery.com/serialize/获取所有表单值(或一些jquery ajaxform插件)。
And please! don't make insecure mysql statements. For gods sake use prepared statements. If you need very basic stuff just use prepared statements or consider https://phpdelusions.net/pdo/pdo_wrapper
拜托!不要制作不安全的mysql语句。为了上帝的缘故,请使用预备陈述。如果你需要非常基本的东西,只需使用预备语句或考虑https://phpdelusions.net/pdo/pdo_wrapper
Also a small tip: before echo json make json header <?php header('Content-type:application/json;charset=utf-8');
还有一个小提示:在echo json之前使json标头
更多相关文章
- 在提交表单之前确定重复值
- 选择无线电输入时,无法提交带有JavaScript功能的表单
- 从列表单击功能获取列表项的值
- if-else语句太多了。他们可以成为全球性的吗?
- 在单页中使用ajax和php上传多个表单数据
- JavaScript表单验证和正则表达式
- 阻止用户在表单字段中输入
- chai-as-promised:单个测试中的多个期望语句
- 放在和中的javascript语句,但是语句不在函数中,这些语句何在被执行