Click here to Skip to main content
15,916,949 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my code below I am using javascript and php for building registration form. There are two files ``register.php`` that contains html code for form, javascript library and javascript code. Second file ``reghand.php`` handles php code for validation and inserting data. Data is inserted correctly into database after form validation but problem appears when validation fails. It's like no javascript comes into action, when validation fails I get redirected to reghand.php where validation errors are shown ``{"username":"inuse","password":"missmatch"}`` these are errors from my reghand.php but I want to use javascript error message that's on ``register.php`` and that too should appear on the form page itself, not on ``reghand.php`` Please help.

<script type="text/javascript" src="./js/jquery.min.js"></script>

  $("form.register").change(function() {
    $.post("register.php", $("form.register").serialize(), function( data ) {
      if( data.flname == "cntbempty" )
      if( data.username == "inuse" )
      if( data.password == "missmatch" )
      if( == "notvalid" )
    }, "json");

<form action="reghand.php" method="post" class="register">
   <input type="text" name="fname" required="required" maxlength="15" placeholder="First Name"/>        
   <input type="text" name="lname" required="required" maxlength="15" placeholder="Last Name"/>
   <p id="name_error" class="error">First name Last name cannot be empty and can be same</p>
   <input type="text" name="username" required="required" maxlength="15" placeholder="Username" class="username" />        
   <p id="username_error" class="error">That Username is unavailable</p>       
   <input type="email" name="email" required="required" maxlength="35" placeholder="Email"/>        
   <p id="email_error" class="error">That Email is already registered</p>       
   <input type="password" name="password" required="required" maxlength="15" placeholder="Password"/>        
   <input type="password" name="password2" required="required" maxlength="15" placeholder="Confirm Password"/>
   <p id="password_error" class="error">Passwords do not match</p>
   <input type="submit" name="reg" value="Sign Up!">

 $fn = ucfirst($_POST['fname']);
 $ln = ucfirst($_POST['lname']);
 $un = $_POST['username'];
 $em = $_POST['email'];
 $pswd = $_POST['password'];
 $pswd2 = $_POST['password2'];
 $sql=$db->prepare("SELECT username FROM users WHERE username=:username");
 $row = $sql->fetch(PDO::FETCH_ASSOC);
 $db_username = $row['username'];
 $usernames = $db_username;
 $data = array();
 if( isset($fn) && isset($ln) ) {
  if( $fn != "" && $ln!="" && $fn == $ln ) {
    $data["flname"] = "cntbempty";
 if( isset($un) ) {
  if ($un == $usernames )  {
    $data["username"] = "inuse";
 if( isset($pswd) && isset($pswd2) ) {
  if( $pswd2 != "" && $pswd != $pswd2 ) {
    $data["password"] = "missmatch";
  if( isset( $em ) ) {
   if( $em != "" && !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["email"] ) ) {
     $data["email"] = "notvalid";
 echo json_encode($data);
  $pswd = password_hash($pswd, PASSWORD_DEFAULT);
  $pswd2 = password_hash($pswd2, PASSWORD_DEFAULT);
  $stmt = $db->prepare("INSERT INTO users (username,first_name,last_name,email,password,password2) VALUES (:username,:first_name,:last_name,:email,:password,:password2)");  
  $stmt->execute( array(':username'=>$un,':first_name'=>$fn,':last_name'=>$ln,':email'=>$em,':password'=>$pswd,':password2'=>$pswd2));
  if ($stmt->rowCount() == 1) {
    header("Location: login.php");
  else {
    echo "error";
Updated 9-Jan-18 19:42pm
Afzaal Ahmad Zeeshan 22-Oct-15 8:09am    
Get the result and see whether you should continue with the request or not. If they are inuse or invalid, then you should reject the propagation of code and stop it from executing any further.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900