473,609 Members | 2,187 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 999

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

Similar topics

5
1899
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 Newsgroups ---= East/West-Coast Server Farms - Total Privacy via Encryption =---
2
3823
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
2790
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 work:
1
3214
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"> <!-- Begin var X = 200; // change the # at the left for a fixed X co-ordinate to accommodate...
3
4939
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 C:\wamp\www\KeyWest\NewRegister.php on line 66... can I get some help. The code I have written is as follows: <?php...
1
27515
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 function and it only worked after I disabled the 2 php header() functions below. The JS unfreeze function...
2
2739
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 + "','" + txtemail.Text + "','" + ddlcountry.SelectedItem.Text + "','" + s1 + "','" +...
0
987
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 validation fails. It's like no javascript comes into action, when validation fails I get redirected...
0
8534
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8188
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8374
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6969
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5502
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4002
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2502
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1630
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1366
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.