NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming
Reload this Page displaying required field errors on the same form using PHP

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.

Advanced Search
5 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 11-13-2006, 10:13 PM THREAD STARTER               #1 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 208
JsteRmX is an unknown quantity at this point
 



displaying required field errors on the same form using PHP


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 Code:
$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 Code:
$success mail($EmailTo$Subject$Body"From: <$EmailFrom>");
????: NamePros.com http://www.namepros.com/programming/257836-displaying-required-field-errors-same-form.html

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.
__________________
+ Domains I have for sale here
JsteRmX is offline  
Old 11-13-2006, 11:52 PM   #2 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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 Code:
 $city = empty($_POST['city']) ? $error['city'] = 'You must enter a city'stripslashes($_POST['city']); 
????: NamePros.com http://www.namepros.com/showthread.php?t=257836
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 Code:
<?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 Code:
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 by Peter; 11-14-2006 at 12:02 AM.
Peter is offline  
Old 11-14-2006, 10:17 AM THREAD STARTER               #3 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 208
JsteRmX is an unknown quantity at this point
 



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!
__________________
+ Domains I have for sale here
JsteRmX is offline  
Old 11-14-2006, 10:19 PM THREAD STARTER               #4 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 208
JsteRmX is an unknown quantity at this point
 



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 Code:
if(is_array($error)) 
????: NamePros.com http://www.namepros.com/showthread.php?t=257836

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.
__________________
+ Domains I have for sale here
JsteRmX is offline  
Old 11-15-2006, 12:14 AM   #5 (permalink)
NamePros Expert
 
Peter's Avatar
Join Date: Nov 2003
Location: Scotland
Posts: 5,074
Peter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond reputePeter has a reputation beyond repute
 


Child Abuse Save The Children Save The Children Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009 Help The Homeless - Holiday 2009
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.
Peter is offline  
Old 11-15-2006, 12:26 AM THREAD STARTER               #6 (permalink)
NamePros Regular
Join Date: Dec 2005
Posts: 208
JsteRmX is an unknown quantity at this point
 



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.
__________________
+ Domains I have for sale here
JsteRmX is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 02:17 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger