简体   繁体   English

AJAX请求始终返回false

[英]AJAX request always return false

Actually, I'm doing a simple php form with some AJAX. 实际上,我正在用一些AJAX做一个简单的php表单。 I've tried to solve the error by myself, but can't figure out what I'm missing. 我试图自己解决错误,但无法弄清我所缺少的内容。

The result is always false, no record are done in the database too 结果始终为假,数据库中也没有记录

The form : 表格 :

<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>

The php scripts : PHP脚本:

config.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: 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;
    }
  }

?>

and finally, the AJAX: 最后,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 for reading ! 感谢阅读!

EDIT : 编辑:

The error came from a wrong way to get the data (No id in the input, while checking for input id's in AJAX) and an input name conflict in the same page. 该错误来自错误的获取数据的方式(在输入中没有ID,同时检查AJAX中的输入ID)和同一页面中的输入名称冲突。 Also I changed the 'echo true/false' in signIn.php to 'echo 1/2' as mentionned by Apokryfos. 另外,正如Apokryfos所说,我将signIn.php中的'echo true / false'更改为'echo 1/2'。

Thank for your help !! 谢谢您帮忙 !!

You're building your post data in the wrong way, #something means the DOM element with id something . 您以错误的方式构建发布数据, #something表示ID为 something的DOM元素。 You need to use the input name attribute: 您需要使用输入name属性:

data = {
  lastname : $("input[name='lastname']").val(),
  firstname : $("input[name='firstname']").val(),
  pseudo : $("input[name='pseudo']").val(),
  password : $("input[name='password']").val(),
}

I will also suggest something additional. 我还将建议其他一些内容。 Use HTTP codes instead of returning a number (That's what the codes are there for and they are universally understood): 使用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)

And the JavaScript 还有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');
      }
  }
})

in ajax part you are using input id like this 在ajax部分,您正在使用这样的输入ID

data = {
      lastname : $('#lastname').val(),
      firstname : $('#firstname').val(),
      pseudo : $('#pseudo').val(),
      password : $('#password').val(),
    }

but in input there is no id for inputs 但在输入中没有输入的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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM