Jump to content

[SOLVED] quick question


NickG21

Recommended Posts

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

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 box

this 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

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 if
if (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

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]
<?php
header('../form1.php')
?>

// you would add the following

<?php
header('../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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.