NickG21 Posted December 29, 2006 Share Posted December 29, 2006 if i am looking to validate all of the information in a form and redisplay the errors that are found on the same page would it be better for my form action to post to ['PHP_SELF'] or whatever or to send it to say validate.php? i am just wondering what one would make it easier to display the error messages next to the information entered or to just change to an error font. i know the font part doesn't matter much but what about redisplaying the messages right next to where the error occurred? thank you Link to comment https://forums.phpfreaks.com/topic/32191-solved-quick-question/ Share on other sites More sharing options...
paul2463 Posted December 29, 2006 Share Posted December 29, 2006 one method I have used is to send it away to a validate.php page which does all the checking for you and assigns an error code to the header back to your page, you then use if(isset($_GET['error1'])) on your main page to show or hide text where you want it to appear say you have a text box for a name on the Form you could have next to the text box [code]<?php if (isset($_GET['error1'])) {echo "<h2><b> Fill This Bit In too You Oaf </b></h2>" } ?>[/code]so when your validate page sees there is no name filled in, it returns with &error1=yes in the URL , it will show the text next to the text boxthis means that your main page is not over large and because if it does end up coming back the only thing to change is the error text it is so fast as to not even flicker Link to comment https://forums.phpfreaks.com/topic/32191-solved-quick-question/#findComment-149425 Share on other sites More sharing options...
NickG21 Posted December 29, 2006 Author Share Posted December 29, 2006 that helps a great deal thanks a lot, but how do i assign the error code to the header back? if you can't tell i am not that proficient in this stuff. thanks Link to comment https://forums.phpfreaks.com/topic/32191-solved-quick-question/#findComment-149439 Share on other sites More sharing options...
kenrbnsn Posted December 29, 2006 Share Posted December 29, 2006 I alway do the validation in the same script that displays the form. This makes displaying errors and messages much easier.Here's a basic outline of the flow:[code]<?php$errors = array();if (isset($_POST['submit'])) { // form has been submitted foreach($_POST as $key => $val) switch($key) { case 'fieldname'://// enter a case statement for each fieldname that has the same validation// if ($_validation_fails) $errors[$key] = "Relavent error message for the $key"; break;//// enter other case statements// }} // end of ifif (empty($errors)) { // no errors found header('location: success.php'); // jump to the success routine exit();}//// display the form here, checking for errors for each field//?>[/code]The above is real rough, but it should give you enough to work with.Ken Link to comment https://forums.phpfreaks.com/topic/32191-solved-quick-question/#findComment-149465 Share on other sites More sharing options...
paul2463 Posted December 29, 2006 Share Posted December 29, 2006 from the validate.php page you need to send the page back to the Form Page if an error exists so to do this you use the <a href="http://uk.php.net/manual/en/function.header.php">header function</a>. at the end of the address such as [code]<?phpheader('../form1.php')?>// you would add the following<?phpheader('../form1.php?error1=yes') // for the first added part to the URL header('../form1.php?error1=yes&error2=yes') // for the subsequent parts to the URL an ampersand for each one?>[/code] Link to comment https://forums.phpfreaks.com/topic/32191-solved-quick-question/#findComment-149502 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.