Query Validation Update

I have a form which the user can update the email address. However I would like to check that the email address has not already in the database.

I want to do this using http://jqueryvalidation.org

I have seen this can be done using http://jqueryvalidation.org/remote-method , but I am struggling if you can please help.

Below is my jQuery code...

			
			$(".Details_Form").validate({
											
    			// Rules
   				rules: {
					first_name: "required",
					last_name: "required",
					company: "required",
					email: {
                                    required: true,
                                    email: true,
									remote: "check_email_script.php"
                                },
					password: "required"
     			},
		
     			// Messages
	 			messages: {
					first_name: "Enter your First Name",
					last_name: "Enter your Last Name",
					company: "Enter your Company",
					email: {
						required: "Enter your Email",
                		               remote: "This has email has already been used! Try another."
            		},	
					password: "Enter your Password"
     			},
		
     			errorElement: "span",
								
      			submitHandler: function() {
					
					// Do something
			
				}
   	
			});
			

Open in new window


And this is my code for the check_email_script.php file...

<?php

mysql_select_db($database_conn_cmyuk, $conn_cmyuk);
$query_rs_users = "SELECT * FROM login_users WHERE user_id != '" . $_POST["cust_id"] . "' AND email = '" . $_POST["email"] . "'";
$rs_users = mysql_query($query_rs_users, $conn_cmyuk) or die(mysql_error());
$row_rs_users = mysql_fetch_assoc($rs_users);
$totalRows_rs_users = mysql_num_rows($rs_users);

if ($totalRows_rs_users > 0) {
    $output = true;
} else {
    $output = false;
}
echo json_encode($output);

?>

Open in new window


How do I change the remote method so it posts the "cust_id" and "email" and validates as required.

Use the below to get the post values...

$( "#cust_id" ).html();
$("#email").val();

Open in new window

petewinterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

sajayj2009Commented:
Can you post your complete form?
petewinterAuthor Commented:
Below is the form code...

<p id="cust_id" style="display:none">590</p>

<div class="OP_Forms Details-Block">

<form action="" method="POST" class="Details_Form">
   
   <p class="first_name"> 
   <label for="first_name">First Name <span>*</span></label>
   <input name="first_name" type="text" id="first_name" size="45" value="<?php echo $row_rs_customer_selected['first_name']; ?>"/>
   </p>
   
   <p class="last_name"> 
   <label for="last_name">Last Name <span>*</span></label>
   <input name="last_name" type="text" id="last_name" size="45" value="<?php echo $row_rs_customer_selected['last_name']; ?>"/>
   </p>
   
   <p class="company">
   <label for="company">Company <span>*</span></label>
   <input name="company" type="text" id="company" size="45" value="<?php echo $row_rs_customer_selected['company']; ?>"/>
   </p>
   
   <p class="email">
   <label for="email">Email <span>*</span></label>
   <input name="email" type="text" id="email" size="45" value="<?php echo $row_rs_customer_selected['email']; ?>"/>
   </p>
         
   <input type="submit" id="Address_Btn" value="Update Details"/>

 </form>
 
 </div>

Open in new window

petewinterAuthor Commented:
Further JQuery code if you want to see the onclick function...

$(document).ready(function() {
		
		var customer_id =  $( "#cust_id" ).html();
				
		$('.ListResults').on('click','.Details_Edit', function(event) {
			event.preventDefault();
									
	
			// Start of Address Add / Update Data	
			
			$(".Details_Form").validate({
											
    			// Rules
   				rules: {
					first_name: "required",
					last_name: "required",
					company: "required",
					email: {
                                    required: true,
                                    email: true,
									remote: "check_email_script.php"
                                },
					password: "required"
     			},
		
     			// Messages
	 			messages: {
					first_name: "Enter your First Name",
					last_name: "Enter your Last Name",
					company: "Enter your Company",
					email: {
						required: "Enter your Email",
                		remote: "This has email has already been used! Try another."
            		},	
					password: "Enter your Password"
     			},
		
     			errorElement: "span",
								
      			submitHandler: function() {
					
					// Do something
			
				}
   	
			});
			
			// End of Address Add / Update Data
				
			});
	
			
});

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

sajayj2009Commented:
$(document).ready(function() {
			$(".Details_Form").validate({
   				rules: {
					first_name: "required",
					last_name: "required",
					company: "required",
					email: {
					  required: true,
					  email: true,
					  remote: {
						url: "check_email_script.php",
						type: "post"
					  },
					password: "required"
					}
     			},
	 			messages: {
					first_name: "Enter your First Name",
					last_name: "Enter your Last Name",
					company: "Enter your Company",
					email: {
						required: "Enter your Email",
                		remote: "This has email has already been used! Try another."
            		},	
					password: "Enter your Password"
     			},
		
     			errorElement: "span",
								
      			submitHandler: function() {
					
					// Do something
			
				}
			});
		});

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petewinterAuthor Commented:
Thanks but how do I post through the two values?
sajayj2009Commented:
You can use jquery serialize or serializeArray functions to club up required fields to post...

But you need to move cust_id from <p element to a hidden input field just after email input field element.

More:
https://api.jquery.com/serializeArray/
https://api.jquery.com/serialize/
petewinterAuthor Commented:
Thanks, but I'm still not sure. Can you please adjust my code to show me how that will work.
petewinterAuthor Commented:
Can the code be something like...

remote: {
										url: "check_email_script.php",
										type: "post",
										data: {
												cust_id: $( "#cust_id" ).html(), 
												email: $("#email").val()
											  }
					  				},

Open in new window

petewinterAuthor Commented:
Thanks for your help. Solve my issues with code below...

email: {
                                    required: true,
                                    email: true,
                                                      remote: {
                                                            url: "check_email_script.php",
                                                            type: "post",
                                                            data: {
                                                                        cust_id: customer_id,
                                                                        email: function()
                                                                {
                                                                    return $("#email").val();
                                                                }
                                                                    }
                                                              }
                                            }
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
jQuery

From novice to tech pro — start learning today.