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;
我基本上按照建议更改了PHP,唯一能想到的其他问题是html或ajax。感谢
您有if ($hello = null) {
您需要将其更改为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;
}
}
关于第二个问题,有两件事是错误的。
将
if ($uname != null && $alert1 = null) {
更改为if ($uname != null && $alert1 === null) {
,您需要将1=改为三个===。再次将
else ($alert1 != null) {
更改为elseif ($alert1 != null) {
,然后再做一个if语句,所以需要做esleif而不是else。error_reporting(E_ALL(;ini_set('display_errors',1(;包括"check_name.php";
$uname=filter_input(input_POST,isset($_POST['uname']((;
if($uname!=null&&$alert1===null({尝试{$conn=新PDO('mysql:host=127.0.0.1;dbname=users','root','password'(;$conn->setAttribute(PDO::ATTR_EMULATE_REPREPARES,false(;$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION(;
$uname=$_POST['uname'];
$upassword=password_hash($_POST['upassword'],password_DEFAULT(;
$stmt='插入
usernames
(uname
,upassword
(值(: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({打印"错误!:"$e->getMessage((。">
";die((;}}elseif($alert1!=null({echo json_encode("此名称已被使用,请选择其他名称。"(;}
$query=null;$conn=null;
?>