somedude somedude - 3 months ago 6
Ajax Question

Edit - updated and fixed variable duplicates, now do I need to quote in AJAX data field? This ajax and PHP gives me error code of 'uname' undefined

I have two things done in this PHP code, one that returns an array of names from the database, and the other that registers given the name doesn't already exist.

Edit - Notice: Undefined variable 'uname' in PHP and AJAX,

function checkNameRegister() {

upassword = document.getElementById("upassword");
uname = document.getElementById("uname");

function checkNameRegister() {

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


};


and PHP:

if ($uname === isset($_POST['uname']) ? $_POST['uname'] : ''){




<?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) {
$alert2 = "This name is not taken.";
echo json_encode($alert2);
}

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

}
}
?>


and

<?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', '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() . "<br/>";
die();
}
}
else ($alert1 != null) {
echo json_encode("This name is already taken, choose another.");
}

$query = null;
$conn =null;
?>


I changed the duplicates, creating an $alert2 value instead of $alert1 duplicate, this doesn't tell me anything now. Trying to get it to register if the name isn't taken, otherwise notify the client that the username is taken, choose another.

Edit: changed to === from == as indicated for $alert1, now I think my ajax is wrong:

function checkNameRegister() {

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


};

Answer

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;

    ?>

Comments