Show error on the same page of registration form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
qasim_353
Forum Newbie
Posts: 9
Joined: Sun Aug 07, 2011 8:41 pm

Show error on the same page of registration form

Post by qasim_353 »

I have a registration form on a php page, i have set the action attribute of the form to the same page, and i have written php code on the same page, i want to show errors on the same page if anyone leaves a blank field in any field of the form and the form should not be submitted if it has errors. Plz help,Thanks.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Show error on the same page of registration form

Post by Dodon »

You can do this in different ways, first of all you can check the forms contents with javascript on Sumbit or per input field by using onblur().
Anotherway is after submitting the form, check the values with PHP and if there are any values of input fields that don't match your criteria write an error message and print this above the form/field.

If you use javascript to check the form, also do this with PHP before posting it to the database in case javascript isn't enabled on the clinet side.
qasim_353
Forum Newbie
Posts: 9
Joined: Sun Aug 07, 2011 8:41 pm

Re: Show error on the same page of registration form

Post by qasim_353 »

But when i do this, it displays the error when i load the page at first time, because the php script is in the same page,
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Show error on the same page of registration form

Post by Dodon »

do something like:

Code: Select all

$errors = 0;

if ($_POST)
{
    // check for errors, if an error occurs change $errors = 1

}

if ($errors == 1)
{
    //print the errors here
}
 
qasim_353
Forum Newbie
Posts: 9
Joined: Sun Aug 07, 2011 8:41 pm

Re: Show error on the same page of registration form

Post by qasim_353 »

Still it displays the error the first time i load the page, i want it to show the error when the form is submitted. Can you give me a working example plz.
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: Show error on the same page of registration form

Post by Dodon »

Show a piece of your code, maybe I can see where you made an error.
qasim_353
Forum Newbie
Posts: 9
Joined: Sun Aug 07, 2011 8:41 pm

Re: Show error on the same page of registration form

Post by qasim_353 »

Code: Select all

<?php
 
// session_start();session_destroy();
// session_start();
if($_POST["first"] && $_POST["last"] && $_POST["email"] && $_POST["email2"] && $_POST["password"] && $_POST["password2"] && $_POST["phone"] && $_POST["address1"] && $_POST["zip"])
{
	if($_POST["password"]==$_POST["password2"] && $_POST["email"]==$_POST["email2"])
	{
	$servername="localhost";
    $username="root";
    $conn=  mysql_connect($servername,$username,"root")or die(mysql_error());
    mysql_select_db("test",$conn);
    $sql="insert into users (name,email,password)values('$_POST[regname]','$_POST[regemail]','$_POST[regpass1]')";
    $result=mysql_query($sql,$conn) or die(mysql_error());		
    print "<h1>you have registered sucessfully</h1>";
   
    print "<a href='index.php'>go to login page</a>";
	}
	else print "passwords or email doesnt match";
}

//header("Location: http://localhost/sc/members/Registerform.html");
else 

	echo 'hello';

$error = 0;
if($_POST["first"] == '')
{
$error  = 1;
}
if($error == 1)
{
print 'hi';	
}
else
print 'hello';
?>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Show error on the same page of registration form

Post by califdon »

qasim_353 wrote:Still it displays the error the first time i load the page, i want it to show the error when the form is submitted. Can you give me a working example plz.
When you use the same script to do 2 different things (one to present a new, blank form and one to check for errors and maybe display errors), you must separate these 2 different logical flows by determining which one you are going to do and branching the code appropriately. You can usually determine this by checking to see if there is any $_POST data. If there is not, the code logic is for sending a blank form, skipping any data checking; if there is $_POST data, the code logic is for checking the data and possibly sending error messages back to the browser.
qasim_353
Forum Newbie
Posts: 9
Joined: Sun Aug 07, 2011 8:41 pm

Re: Show error on the same page of registration form

Post by qasim_353 »

Please see the code above
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Show error on the same page of registration form

Post by twinedev »

If the above doesn't help, you may want to look at the sample code I put here:

viewtopic.php?f=1&t=130159&p=654640#p654640

It gives a form that does Server Side validation, and not only finds there was an error, but you can customize the error and displays them back on the form, along with data they already entered.

-Greg
Post Reply