473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Registration form using javascript and php

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, I was never supposed to redirect to reghand.php. Please help.

register.php
Expand|Select|Wrap|Line Numbers
  1.     <script type="text/javascript" src="./js/jquery.min.js"></script>
  2.            <script>
  3.             $(document).ready(function(){
  4.       $("form.register").change(function() {
  5.         $.post("register.php", $("form.register").serialize(), function( data ) {
  6.           if( data.flname == "cntbempty" )
  7.             $("p#name_error").slideDown();
  8.           else
  9.             $("p#name_error").hide();
  10.           if( data.username == "inuse" )
  11.             $("p#username_error").slideDown();
  12.           else
  13.             $("p#username_error").hide();
  14.           if( data.password == "missmatch" )
  15.             $("p#password_error").slideDown();
  16.           else
  17.             $("p#password_error").hide();
  18.           if( data.email == "notvalid" )
  19.             $("p#email_error").slideDown();
  20.           else
  21.             $("p#email_error").hide();
  22.         }, "json");
  23.       });
  24.     });
  25.     </script>
  26.        <form action="reghand.php" method="post" class="register">
  27.        <fieldset>
  28.        <legend>Registration</legend>
  29.        <input type="text" name="fname" required="required" maxlength="15" placeholder="First Name"/>        
  30.        <input type="text" name="lname" required="required" maxlength="15" placeholder="Last Name"/>
  31.        <p id="name_error" class="error">First name Last name cannot be empty and can be same</p>
  32.        <input type="text" name="username" required="required" maxlength="15" placeholder="Username" class="username" />        
  33.        <p id="username_error" class="error">That Username is unavailable</p>       
  34.        <input type="email" name="email" required="required" maxlength="35" placeholder="Email"/>        
  35.        <p id="email_error" class="error">That Email is already registered</p>       
  36.        <input type="password" name="password" required="required" maxlength="15" placeholder="Password"/>        
  37.        <input type="password" name="password2" required="required" maxlength="15" placeholder="Confirm Password"/>
  38.        <p id="password_error" class="error">Passwords do not match</p>
  39.        <input type="submit" name="reg" value="Sign Up!">
  40.      </fieldset>
  41.     </form>
reghand.php
Expand|Select|Wrap|Line Numbers
  1.     <?php
  2.     if(isset($_POST['reg'])){
  3.      $fn = ucfirst($_POST['fname']);
  4.      $ln = ucfirst($_POST['lname']);
  5.      $un = $_POST['username'];
  6.      $em = $_POST['email'];
  7.      $pswd = $_POST['password'];
  8.      $pswd2 = $_POST['password2'];
  9.      $sql=$db->prepare("SELECT username FROM users WHERE username=:username");
  10.      $sql->execute(array(':username'=>$un));
  11.      $row = $sql->fetch(PDO::FETCH_ASSOC);
  12.      $db_username = $row['username'];
  13.      $usernames = $db_username;
  14.      $data = array();
  15.      if( isset($fn) && isset($ln) ) {
  16.       if( $fn != "" && $ln!="" && $fn == $ln ) {
  17.         $data["flname"] = "cntbempty";
  18.        }
  19.       }
  20.      if( isset($un) ) {
  21.       if ($un == $usernames )  {
  22.         $data["username"] = "inuse";
  23.        }
  24.       }
  25.      if( isset($pswd) && isset($pswd2) ) {
  26.       if( $pswd2 != "" && $pswd != $pswd2 ) {
  27.         $data["password"] = "missmatch";
  28.        }
  29.       }
  30.       if( isset( $em ) ) {
  31.        if( $em != "" && !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["email"] ) ) {
  32.          $data["email"] = "notvalid";
  33.        }
  34.       }
  35.      if(!empty($data))
  36.      {
  37.      echo json_encode($data);
  38.      die;
  39.      }
  40.       else{
  41.       $pswd = password_hash($pswd, PASSWORD_DEFAULT);
  42.       $pswd2 = password_hash($pswd2, PASSWORD_DEFAULT);
  43.       $stmt = $db->prepare("INSERT INTO users (username,first_name,last_name,email,password,password2,) VALUES (:username,:first_name,:last_name,:email,:password,:password2,)");  
  44.       $stmt->execute( array(':username'=>$un,':first_name'=>$fn,':last_name'=>$ln,':email'=>$em,':password'=>$pswd,':password2'=>$pswd2));
  45.       }
  46.       if ($stmt->rowCount() == 1) {
  47.         header("Location: login.php");
  48.       } 
  49.       else {
  50.         echo "error";
  51.       }
  52.     }
  53.     ?>
Oct 21 '15 #1
0 970

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Frederik S | last post by:
Hello, I'm making a nice little login box in Javascript but having problems Posting the value inside a textfield. In a nutshell: I have a function: function getPostData (value)
1
by: MrMaxPower | last post by:
Hi, I have an Outlook form which has VB validation script running behind it. I need to launch this form from a webpage. When opening from an <a> anchor, it opens the form OK, but not does not...
1
by: macintoshhondo | last post by:
Hi ! i am a newbie and dont know javascript much. what i really need is a simple javascript code that can insert number in the value section of the different forms from the one form. FORM 1:...
6
by: bizt | last post by:
Hi, First let me point out I have googled this and also must have done this operation a dozen times (altho a few years ago) but for some reason I cant get a form to submit using javascript .....
4
by: phpuser123 | last post by:
I want to submit my form using javascript but the prob I am facing here is when using this method,the form is being submitted irrespective of the return value of onsubmit .My script is ..... ...
1
by: phpuser123 | last post by:
I have just started javascript and I want to submit a form usaing javasript.I came up with the following codes but I cannot see what mistake i've committed.. <html> <head> <script...
5
by: nesha | last post by:
Hi, I have a html page in which im using a form. In that form i placed a div element within the form. That div element has another div element within that. how to get the innermost div element value...
21
by: bheema1990 | last post by:
I want to save the form using this makeFile() function.I want it to save the whole form in csv format..How can i make it read the whole file...TQ...n Am i doing it correctly?Please help me solve this...
3
by: komathi043 | last post by:
i need to validate a form using javascript.But it should not validate with mssbox.i need different method for displaying err msg
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.