doutan5844 2016-08-31 13:01
浏览 17

Ajax无法正常工作 - 无论如何都发布了固定的PHP

Html:

<form action="registerusers.php" method="POST">
    <input id= "uname" name="uname" placeholder="username" required>
    <input id="upassword" name="upassword" type="password"   placeholder="password" required>
    <button id="register" type="button" class="btn btn-primary btn-lg outline" onclick="javascript: validateForm();checkNameRegister();" >Register</button>

Ajax:

function checkNameRegister() {

$.ajax({
          url: "registerusers.php",
          data: { uname : "uname" , upassword : "upassword"},
          type: "POST",
          async: false,
          dataType: 'text',
          success: function(responseText) {
            alert(responseText);
          }
    });


};

php:

<?php

header('Content-Type: application/json');

error_reporting(E_ALL);
ini_set('display_errors', 1);

$alert1 = null;
$uname = filter_input(INPUT_POST, isset($_POST['uname'])); 

if ($uname === $_POST['uname'] ? $_POST['uname'] : ''){
try {
$conn = new PDO('mysql:host=127.0.0.1;dbname=users', 'root', 'redcardinal');
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = 'SELECT * FROM `usernames` WHERE `uname` = :uname';
$stmt = $conn->prepare($query);
$stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));



$stmt->execute();
$hello = array(array());
if ($hello = $stmt->fetch(PDO::FETCH_ASSOC) ? $hello != null : ''){
    $alert1 = "This name is taken.";
echo json_encode($alert1);
$conn = null;
$query = null;
}
elseif ($hello === null) {
    $alert2 = "This name is not taken.";
 echo json_encode($alert2);
}

}
catch (PDOException $e) { echo json_encode($e);
$conn = null;
$query = null;

}

EDIT: WILL PUT ADDITIONAL elseif HERE.

}
?>

<?php 

header('Content-Type: application/json');

error_reporting(E_ALL); 
ini_set('display_errors', 1); 
include 'check_name.php'; 

$uname = filter_input(INPUT_POST, isset($_POST['uname'])); 

if ($uname != null && $alert1 === null) { 
try {
$conn = new PDO  ('mysql:host=127.0.0.1;dbname=users', 'root', 'redcardinal'); 
$conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); 
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

$uname = $_POST['uname']; 

$upassword = password_hash(filter_input(INPUT_POST, isset($_POST['upassword'])), PASSWORD_DEFAULT); 



$stmt = 'INSERT INTO `usernames` (`uname`, `upassword`) VALUES (:uname,:upassword)'; 
$query = $conn->prepare($stmt); 
$query->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname)); 
$query->bindParam(":upassword",$upassword, PDO::PARAM_STR, strlen($upassword)); 



$query->execute(); 
echo json_encode("Username/Password successfully created.");
} 
catch (PDOException $e) {
print "Error!: " . $e->getMessage() . "<br/>";
die();
}
}
elseif ($alert1 != null) { 

echo json_encode("Username/Password taken."); 
}

$query = null; 
$conn =null; 

I basically changed the PHP as per suggested, the only other problem I can think of is the html or ajax. Thanks

  • 写回答

1条回答 默认 最新

  • drplww2505 2016-08-31 13:18
    关注

    You have your if ($hello = null) {

    you need to change it to if ($hello === null) {

    <?php
    header('Content-Type: application/json');
    
    
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $alert1 = null;
    
    if ($uname = isset($_POST['uname']) ? isset($_POST['uname']) : ''){
    try {
    $conn = new PDO('mysql:host=127.0.0.1;dbname=users', 'root', 'password');
    $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $query = 'SELECT * FROM `usernames` WHERE `uname` = :uname';
    $stmt = $conn->prepare($query);
    $stmt->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname));
    
    
    
    $stmt->execute();
    $hello = array(array());
    if ($hello = $stmt->fetch(PDO::FETCH_ASSOC) && $hello != null){
        $alert1 = "This name is taken.";
    echo json_encode($alert1);
    $conn = null;
    $query = null;
    }
    if ($hello === null) {
        $alert1 = "This name is not taken.";
     echo json_encode($alert1);
    }
    
    }
    catch (PDOException $e) { echo json_encode($e);
    $conn = null;
    $query = null;
    $alert1 = null;
    }
    
    
    
     }
    

    and about the second one, two things are wrong.

    1. change if ($uname != null && $alert1 = null) { to if ($uname != null && $alert1 === null) { you need to make three === insted of 1 =.

    2. again change else ($alert1 != null) { to elseif ($alert1 != null) { you make another if statement so you need to do esleif instead of else.

      error_reporting(E_ALL); ini_set('display_errors', 1); include 'check_name.php';

      $uname = filter_input(INPUT_POST, isset($_POST['uname']));

      if ($uname != null && $alert1 === null) { try { $conn = new PDO ('mysql:host=127.0.0.1;dbname=users', 'root', 'password'); $conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

      $uname = $_POST['uname'];

      $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

      $stmt = 'INSERT INTO usernames (uname, upassword) VALUES (:uname,:upassword)'; $query = $conn->prepare($stmt); $query->bindParam(":uname",$uname, PDO::PARAM_STR, strlen($uname)); $query->bindParam(":upassword",$upassword, PDO::PARAM_STR, strlen($upassword));

      $query->execute(); } catch (PDOException $e) { print "Error!: " . $e->getMessage() . "
      "; die(); } } elseif ($alert1 != null) {

      echo json_encode("This name is already taken, choose another."); }

      $query = null; $conn =null;

      ?>

    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line