RaomB RaomB - 5 months ago 25
Javascript Question

Register form insert the record with "sorry,email is exist" aleart

in my register form i want to validate the user input and check if the email is exist or not in the data base , the php insert the value if the email not exist in db but it still give me the "sorry,email is exist" alert and not enter to profile.html should i add after insert a select query?

$('#sign-up').live('pageshow', function() {
// Declare editMode : true when a user is logged in , false otherwise
var editMode = globals.currentUser != null;

if(editMode) {
$("#txt-first-name").val(globals.currentUser.firstName);
$("#txt-last-name").val(globals.currentUser.lastName);
$("#txt-email").val(globals.currentUser.email);
}

$("#btn-submit").on("click", function(event){
event.preventDefault();

var firstName = $("#txt-first-name").val();
if(!firstName || firstName == "") {
alert("enter first name");
$("#txt-first-name").focus();
return;
}

var lastName = $("#txt-last-name").val();
if(!lastName || lastName == "") {
alert("enter last name");
$("#txt-last-name").focus();
return;
}

var email = $("#txt-email").val();
var filter = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

if(!email || email == ""|| !filter.test(email)) {
alert("enter correct email");
$("#txt-email").focus();
return;
}

var password = $("#txt-password").val();
if(!editMode && (!password || password == "")) {
alert("enter password");
$("#txt-password").focus();
return;
}

if(password.indexOf(" ") > -1) {
alert("password must not have space");
$("#txt-password").focus();
return;
}

var passwordConfirm = $("#txt-password-confirm").val();
if(!editMode && (!passwordConfirm || passwordConfirm == "")) {
alert("confirm password");
$("#txt-password-confirm").focus();
return;
}

// Check if password and password confirmation are equal
if(passwordConfirm !== password) {
alert("passwords not equal to each other");
$("#txt-password-confirm").focus();
return;
}

var data = {
email: email,
password: password == "" ? "" : btoa(password),
firstName: firstName,
lastName: lastName,
id: (globals.currentUser != null ? globals.currentUser.userid : null)
};

$.getJSON(globals.serviceHost + (editMode ? "editprofile.php" : "register.php"), data,
// on success callback
function (result) {
console.log("result : ");
console.log(result);
if(editMode) {
globals.currentUser.firstName = firstName;
globals.currentUser.lastName = lastName;
globals.currentUser.email = email;
alert("information has been edited");
window.history.back();
}

if(!result.user || result.user == "data") {
alert("sorry,email is exist");
$("#txt-email").focus();
return;

}


globals.currentUser = result.user;
alert("register successfully ");
$.mobile.navigate("profile.html");

});

});
});


//php

<?php
include "config.php";
header('Content-Type: application/json');
$email = $_GET['email'];
$password = $_GET['password'];
$firstName = $_GET['firstName'];
$lastName = $_GET['lastName'];
$data=array();

$q = mysqli_query($con,"SELECT * FROM users WHERE email='$email' ");

$num_rows = mysqli_num_rows($q);
if($num_rows == 0){

$qr = mysqli_query($con,"INSERT INTO users (email, password, firstName, lastName) VALUES ('$email', '$password', '$firstName', '$lastName') ");

echo '{"inserted":"true"}';

}
else {
$data = "data";
echo '{"user":'. json_encode($data) .'}';
}


?>

Answer Source

This condition

 if(!result.user || result.user == "data") 

always return true because is always true based on your responses

Users does not exist:

 echo '{"inserted":"true"}';

User exists:

 $data = "data";
 echo '{"user":'. json_encode($data) .'}'; 

Your condition should be something like

if(result.user == "data")