PHP MySQL Tutorial
Learn PHP and MySQL

Comments: Form Validation Using PHP

Page Details

Published by:
admin
on 12-18-2008
3 people found this article useful.

Form Validation Using PHP

Sort by: Published Date | Most Recent | Most Useful
1 2 Next >
By: edw Posted on 12-20-2008 2:58 PM

Wow, this was some great stuff. I was looking at a general concept for validating an email address.

I have a customer table that when they create an account, I don't want repeat entries. In other words,

if the enter their email address and it already exists, I want it to return with,  there is an account with this email address already............go to forget your user name/ password.

Should I use if ($_POST[emai]) array to check? Should I do a select statement to the mySQL database, and if returns with a row, so it is true, give the error message?

Any code or advice would be great.

By: edw Posted on 12-20-2008 8:06 PM

Here is the function I created  to check the database for a dupe email address:

// if returns true success, false means there were dupe emails.

function checkEmailDupes($email) {

global $connection;

$UCemail = strtoupper($email);

$query = "SELECT email FROM customer

WHERE upper(email) =

'{$UCemail}'";

$checkEmail = mysql_query($query, $connection);

confirm_query($checkEmail);

$numRows = mysql_num_rows($checkEmail);

if($numRows==0){

return true;

} else {

return false;

}              

}

then i use the function:

$EmNoDup =false;

if(checkEmailDupes($email)){

$EmNoDup =true;

}else {

//header("Location:cform.php");

//exit;

echo "<script>alert('dupe email')</script>";

}

if(empty($errors) && $EmNoDup == true ) {

$query = "INSERT INTO customer (............................and so on.

Does this look like a generally accepted code? I am quite new.............thanks KP

By: Michael Clark Posted on 01-30-2009 1:15 PM

Looks good enough to me.

By: magna Posted on 03-01-2009 8:16 AM

Concerning clientside validation I would propose to call the validation function when submitting the form not when clicking the submitbutton, cause you can also submit a form by just typing <enter> when the focus is on a form field.

So this seems more proper to me

<form method="post" name="msgform" onsubmit="return checkForm();">

<input type="submit">

</form>

than this

<form method="post" name="msgform">

<input type="submit" onclick="return checkForm();">

</form>

By: Tammy Creaser Posted on 03-27-2009 1:42 AM

I really found this useful except the links to the files no longer work.  I would really like to see the files.  Can anyone help me here as I am a newbie to php?

Powered by Community Server (Non-Commercial Edition), by Telligent Systems