473,513 Members | 3,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use jQuery to show errors while submitting registration form.

19 New Member
In the code below I want to show error messages using jQuery for registration form errors. Problem is this code is simply inserting data into database without checking any errors or showing error message and redirects to login page. So where I am going wrong or what I am missing? I have included jQuery library in original code.
Expand|Select|Wrap|Line Numbers
  1. if(isset($_POST['reg'])){
  2.   $fn = ucfirst($_POST['fname']);
  3.   $ln = ucfirst($_POST['lname']);
  4.   $un = $_POST['username'];
  5.   $em = $_POST['email'];
  6.   $pswd = $_POST['password'];
  7.   $pswd2 = $_POST['password2'];
  8.  
  9.   $sql=$db->prepare("SELECT username FROM userss 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.  
  15.   $data = array( "flname"=>"", "username" => "", "email" => "", "password" => "");
  16.   if( isset($fn) && isset($ln) ) {
  17.     if( $fn != "" && $ln!="" && $fn == $ln ) {
  18.       $data["flname"] = "cntbempty";
  19.     }
  20.   }
  21.   if( isset($un) ) {
  22.     if( in_array( $un, $usernames ) ) {
  23.       $data["username"] = "inuse";
  24.     }
  25.   }
  26.   if( isset($pswd) && isset($pswd2) ) {
  27.     if( $pswd2 != "" && $pswd != $pswd2 ) {
  28.       $data["password"] = "missmatch";
  29.     }
  30.   }
  31.   if( isset( $em ) ) {
  32.     if( $em != "" && !preg_match( "/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $_POST["email"] ) ) {
  33.       $data["email"] = "notvalid";
  34.     }
  35.   }
  36.   echo json_encode( $data );
  37.  
  38.   $pswd = password_hash($pswd, PASSWORD_DEFAULT);
  39.   $pswd2 = password_hash($pswd2, PASSWORD_DEFAULT);
  40.   $stmt = $db->prepare("INSERT INTO userss (username,first_name,last_name,email,password,password2,) VALUES (:username,:first_name,:last_name,:email,:password,:password2,)");  
  41.   $stmt->execute( array(':username'=>$un,':first_name'=>$fn,':last_name'=>$ln,':email'=>$em,':password'=>$pswd,':password2'=>$pswd2));
  42.  
  43.   if ($stmt->rowCount() == 1) {
  44.     header("Location: login.php");
  45.   } 
  46.   else {
  47.     echo "error";
  48.   }
  49. }
  50. ?>
jQuery code is:
Expand|Select|Wrap|Line Numbers
  1. $(document).ready(function(){
  2.   $("form.register").change(function() {
  3.     $.post("register.php", $("form.register").serialize(), function( data ) {
  4.       if( data.flname == "cntbempty" )
  5.         $("p#name_error").slideDown();
  6.       else
  7.         $("p#name_error").hide();
  8.       if( data.username == "inuse" )
  9.         $("p#username_error").slideDown();
  10.       else
  11.         $("p#username_error").hide();
  12.       if( data.password == "missmatch" )
  13.         $("p#password_error").slideDown();
  14.       else
  15.         $("p#password_error").hide();
  16.       if( data.email == "notvalid" )
  17.         $("p#email_error").slideDown();
  18.       else
  19.         $("p#email_error").hide();
  20.     }, "json");
  21.   });
  22. });
Oct 11 '15 #1
0 996

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

Similar topics

5
1887
by: Don | last post by:
I have a need to submit a form, but don't need the user to click on a button. How do I do this? Is there some way, using JavaScript, to setup a <form> tag to do this? Thanks, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! >100,000...
2
3815
by: varun | last post by:
Hi, I am using goahead webserver...i have a form, in acction attribute i am calling a c function.. after submitting the form i need to close the window and i need to reload the window which navigated to this window..... i used this javasript ....... function doUnLoad()
4
2785
by: Super Steve | last post by:
I'm trying to write a little script that disables the submit button on a form, changes the text of the button, and then submits the form. Can anyone tell me why this works ok: <input type="button" value="Submit" onClick="this.disabled=true; this.value = 'Submitting...'; this.form.submit();"> But when I try to make a function it doesn't...
1
3195
by: dittu | last post by:
How to close the popup window when submitting the form? I have a sample.jsp. In that page one button is there. when button pressed, open popup window contains one "text box" and one " submit button". when "submit" button pressed, call the "servlet" in this popup window and then close the "popup" window. <SCRIPT LANGUAGE="JavaScript"> ...
3
4934
by: Rana Chakra | last post by:
I am trying to send an automatic mail containing the account activation link to the person who is filling up the registration form after he/she clicks on Create account button. But every time it giving an error .... Warning: mail() : SMTP server response: 530 5.7.0 Must issue a STARTTLS command first. w42sm3532376wfh.15 in...
1
27507
by: blumonde | last post by:
Hello, I read an article on "how to prevent users from submitting a form twice." at the url http://www.4guysfromrolla.com/webtech/100406-1.shtml I was able to freeze the web page but was not able to unfreeze the web page. I am using PHP with javascript. Nothing happens when my code executes the "unfreeze" js function. I tested this js...
2
2730
by: developerquery | last post by:
When a user fills all the required information in a registration form then how do I record the date of submitting the form using Sql Server 2005? My insert query is : con.Open(); SqlCommand command = new SqlCommand("insert into data (UserID,Name,Email,Country,Date,Multiselect,Gender,Pincode)values('" + txtuserid.Text + "','" + txtname.Text...
0
975
by: harman30 | last post by:
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...
0
7177
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7394
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7559
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7542
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5701
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4756
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3248
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
811
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
470
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.