实际上,我正在用一些AJAX做一个简单的php表单。 我试图自己解决错误,但无法弄清楚我错过了什么。
结果始终为false,数据库中也没有记录
表格:
<form id="signUpForm" action="" method="POST"></div>
<div><input type="text" name="lastname" placeholder="lastname"></div>
<div><input type="text" name="firstname" placeholder="firstname"></div>
<div><input type="text" name="pseudoUp" placeholder="pseudo"></div>
<div><input type="password" name="passwordUp" placeholder="password"></div>
<div><input type="submit" name="signUpForm" value="signup"></div>
<div class="signUpMsg"></div>
</form>
php脚本:
config.php:
<?php
session_start();
define('MYSQL_HOST', 'localhost');
define('MYSQL_USER', ' ');
define('MYSQL_PASSWD', ' ');
define('MYSQL_DB', 'php');
try {
$PDO = new PDO('mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB, MYSQL_USER, MYSQL_PASSWD);
$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$PDO->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
} catch (PDOException $e) {
$e->getMessage();
}
?>
signUp.php:
<?php
require_once 'config.php';
if($_POST["lastname"] != "" && $_POST["firstname"] != "" && $_POST["pseudo"] != "" && $_POST["password"] != ""){
$req = $PDO->prepare("INSERT INTO users (lastname, firstname, pseudo, password) VALUES(:lastname, :firstname, :pseudo, :password)");
$req->bindValue(':lastname', $_POST["lastname"]);
$req->bindValue(':firstname', $_POST["firstname"]);
$req->bindValue(':pseudo', $_POST["pseudo"]);
$req->bindValue(':password', sha1($_POST["password"]));
if ($req->execute()){
echo 1;
}else{
echo 2;
}
}
?>
最后,AJAX:
$(function(){
$('#signUpForm').on('submit', function(e){
e.preventDefault();
// Undo mistake, thank to @apokryfos
data = {
lastname : $("input[name='lastname']").val(),
firstname : $("input[name='firstname']").val(),
pseudo : $("input[name='pseudo']").val(),
password : $("input[name='password']").val(),
}
$.ajax({
method : "POST",
url : "php/signUp.php",
data : data,
success : function(res){
if(res == 1){
$('.signUpMsg').html('Sign up done !');
}else{
$('.signUpMsg').html('Sign up fail');
}
}
})
})
})
Thanx阅读!
编辑:
错误来自于获取数据的错误方式(输入中没有id,同时检查AJAX中的输入id)和同一页面中的输入名称冲突。我也改变了回声真/假&#39;在signIn.php中回显1/2&#39;正如Apokryfos所提到的那样。
感谢您的帮助!!
答案 0 :(得分:5)
您以错误的方式构建帖子数据,#something
表示 id something
的DOM元素。您需要使用输入name
属性:
data = {
lastname : $("input[name='lastname']").val(),
firstname : $("input[name='firstname']").val(),
pseudo : $("input[name='pseudo']").val(),
password : $("input[name='password']").val(),
}
我还会建议一些额外的东西。使用HTTP代码而不是返回一个数字(这就是代码的用途,它们是普遍理解的):
if($_POST["lastname"] != "" && $_POST["firstname"] != "" && $_POST["pseudo"] != "" && $_POST["password"] != ""){
$req = $PDO->prepare("INSERT INTO users (lastname, firstname, pseudo, password) VALUES(:lastname, :firstname, :pseudo, :password)");
$req->bindValue(':lastname', $_POST["lastname"]);
$req->bindValue(':firstname', $_POST["firstname"]);
$req->bindValue(':pseudo', $_POST["pseudo"]);
$req->bindValue(':password', sha1($_POST["password"]));
if ($req->execute()){
echo 1;
}else{
http_response_code(500); //server error
echo 2;
}
}
http_response_code(400); //Client error (didn't send the correct fields)
和JavaScript
$.ajax({
method : "POST",
url : "php/signUp.php",
data : data,
success : function(res){
$('.signUpMsg').html('Sign up done !'); //Only runs this when successful
},
error: function (xhr, textStatus) {
if (xhr.status == 500) {
$('.signUpMsg').html('Sign up fail');
} else if (xhr.status == 400) {
$('.signUpMsg').html('Please fill the form properly');
}
}
})
答案 1 :(得分:0)
在ajax部分,你正在使用像这样的输入id
data = {
lastname : $('#lastname').val(),
firstname : $('#firstname').val(),
pseudo : $('#pseudo').val(),
password : $('#password').val(),
}
但在输入中没有输入ID
<div><input type="text" name="lastname" placeholder="lastname"></div>
<div><input type="text" name="firstname" placeholder="firstname"></div>
<div><input type="text" name="pseudo" placeholder="pseudo"></div>
<div><input type="password" name="password" placeholder="password"></div>