returning multiple errors in the ajax response is proving troublesome, if multiple errors are echoed, 1&2 for example becomes 12. What would be the best way to return the response data, in an array? Any help is much appreciated, thanks in advance.
This is the function for POSTING the form to the PHP validator
$("document").ready(function() {
$("#btn-signup").click(function() {
var first_name = $("#fname_up").val();
var last_name = $("#lname_up").val();
var vemail = $("#email_up").val();
var password = $("#password_up").val();
var cpassword = $("#cpassword_up").val();
var dataString = 'firstname='+first_name+'&lastname='+last_name+'&email='+vemail+'&password='+password+'&cpassword='+cpassword;
$.ajax({
type: "POST",
url: "-signup.php",
data: dataString,
success: function(response){
if (response == 1) {
console.log("1");
}
if (response == 2) {
console.log("2");
}
if (response == 3) {
console.log("3");
}
if (response == 4) {
console.log("4");
}
if (response == 5) {
console.log("5");
}
if (response == 6) {
console.log("6");
}
if (response == 7) {
console.log("7");
}
if (response == 12) {
console.log("You got to here");
}
}
});
return false;
});
});
This is the PHP form validator.
<?php
session_start();
require_once "database.php";
db_connect();
$errors = array();
// If request is a form submission
if($_SERVER['REQUEST_METHOD'] == 'POST'){
// Validation
// Check first_name is non-blank
if(0 === preg_match("/\S+/", $_POST['firstname'])){
echo "1";
}
// Check last_name is non-blank
if(0 === preg_match("/\S+/", $_POST['lastname'])){
echo "2";
}
// Check email is valid (enough)
if(0 === preg_match("/.+@.+\..+/", $_POST['email'])){
echo "3";
}
// Check password is valid
if(0 === preg_match("/.{6,}/", $_POST['password'])){
echo "4";
}
// Check password confirmation_matches
if(0 !== strcmp($_POST['password'], $_POST['cpassword'])){
echo "5";
}
// If no validation errors
if(0 === count($errors)){
// Sanitize first, last, and email
$firstname = mysql_real_escape_string($_POST['firstname']);
$lastname = mysql_real_escape_string($_POST['lastname']);
$email = mysql_real_escape_string($_POST['email']);
$password = mysql_real_escape_string($_POST['password']);
$cpassword = mysql_real_escape_string($_POST['cpassword']);
// Generate pseudo-random salt
$salt = sha1(microtime() . $_POST['password']);
// Generate password from salt
$password = sha1($salt . $_POST['password']);
// Insert user into the database
$query = "INSERT INTO.......
$result = mysql_query($query);
if(mysql_errno() === 0){
// Registration successful
$user_id = mysql_insert_id();
log_in($user_id);
echo "9";
} else if (preg_match("/^Duplicate.*email.*/// get rid of the two lines beforei", mysql_error())){
echo "10";
}
}
}
Which are the errors you get? Is your AJAX URL correctly assigned?
I think you should give the absolute path to the “-signup.php” file. Something like /path/to/signup-file.php
Why did you used if(0 === count($errors)) and not if(count($errors) == 0), also for the above verifications. You can use if(empty($errors)).
For more help please write us the errors you have