Unstoppable Domains

Displaying required field errors on the same form using PHP

Spaceship Spaceship
Watch

JsteRmX

Established Member
Impact
2
right now i have a form on an html page with form action to the php script page. the php page checks to make sure all the required fields were entered, and if not it will display an error message when the user submits the form. the problem is, that the error message appears on a new page, forcing the user to hit 'back' to go back and fix their mistakes.

i am looking for a way to have the errors show up right next to the required fields if left blank when they hit submit. here is a sample of how i have my error checking set up right now:

PHP:
$city = empty($_POST['city']) ? die ("ERROR: You must enter a city.") : stripslashes($_POST['city']);

upon successful completion of the form, it will send an email to me.

PHP:
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");

if ($success){
  print "Thank you ... blah blah blah";
}
else{
  print "Sorry, blah blah blah";
}

Is this setup not going to work to make the errors showup next to the missing fields on the form? How can i do this with the way i have my code now? If not, how would i go about doing this at all? someone wrote this to show me how it works : http://pastebin.com/823891 but from what i understand, this wouldnt really work with how i wrote my script, because I dont ONLY check for errors, I echo the error in with die.

Any help would be great. Thanks.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
no the method you have stated there will terminate the script when an error is found.

what you need to do is something like the following:-

PHP:
 $city = empty($_POST['city']) ? $error['city'] = 'You must enter a city': stripslashes($_POST['city']);

Now in your html form you will need to edit the code where you wish the error to be displayed and add something like the following:-

PHP:
<?PHP
if($error['city'])
{
echo $city_error;
}
?>

You would of course have to do this for every form field you wish to display the error for (obviously changing the array index $error['city'] in both cases to suit. When deciding wether to send the email or display the form again you would simply do the following:-

PHP:
if(is_array($error))
{
OUTPUT FORM HERE
}
else
{
NO ERROR EXISTS SO SEND THE EMAIL
}

With regard to the pastebin code in all honesty it i overkill, using sessions for this is of no real value at all.
 
Last edited:
0
•••
ok thanks alot for your reply. i dont have time to try out what you said right now, but i understand all you wrote and i believe ill get it to work fine.

I'll post back later after i fix up my code to let you know if it works for me.

Thanks!
 
0
•••
hmm i tried what you said, but i cant get the form to show where it says

if(is_array($error))
{
OUTPUT FORM HERE
}
else
{
NO ERROR EXISTS SO SEND THE EMAIL
}

How should i get my form to show again?
I tried echoing and printing it out, but i get errors when i do that. I am able to have it just print out plain text though. But my form does not work properly. All i am doing to attempt to get the form there, is copy and pasting the form from the html page including with the $city_error code.

any ideaS?

ok i found a way around this.

I copied and pasted my html form into a blank document saved as formsubmit.php

and used the following code:

PHP:
if(is_array($error)) 
{ 
print "Required fields are missing.";
include "formsubmit.php";
} 
else 
{ 
NO ERROR EXISTS SO SEND THE EMAIL 
}

this shows the new form with the errors where i want them correctly.

Thanks for you help.
 
0
•••
using the includes is a perfectly fine way to do it, another way would be within the ifd statement to come out of php output the form then jump back into php. The include yuou have done however will be easier for you to manage.
 
0
•••
it is, thanks for helping with this, i just finished updating my site with all this new functionality - its pretty sweet.

yeah i like the includes better, keeps the code shorter and easier to manage like you said.
 
0
•••
Dynadot — .com Registration $8.99Dynadot — .com Registration $8.99
Appraise.net

We're social

Unstoppable Domains
Domain Recover
DomainEasy — Payment Flexibility
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back