Show error on the same page of registration form
Moderator: General Moderators
Show error on the same page of registration form
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.
Re: Show error on the same page of registration form
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.
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.
Re: Show error on the same page of registration form
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,
Re: Show error on the same page of registration form
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
}
Re: Show error on the same page of registration form
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.
Re: Show error on the same page of registration form
Show a piece of your code, maybe I can see where you made an error.
Re: Show error on the same page of registration form
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';
?>Re: Show error on the same page of registration form
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 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.
Re: Show error on the same page of registration form
Please see the code above
Re: Show error on the same page of registration form
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
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