Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Redirect

Status
Not open for further replies.

CliffLandin

Programmer
Nov 14, 2004
81
US
I am trying to make a page that will check to see if all the fields in a form are not blank and if any are blank the page will send you back to the form and give you an error message. I know how to check the form fields, that's easy, but I don't know how to redirect a page other than using the header function. Is there any other way to redirect a page using PHP?

Thanks in advance for any help that you can offer.

When in doubt, go flat out!

 
here is the code that I am using

if (($naErr != 0) || ($moErr != 0) || ($pnErr != 0) || ($dcErr != 0) || ($emErr != 0)){
$reDir = " header("Location: $reDir");
exit;
}

the if/or function is confirming that an error occured in one of the form fields.

But I receive an error message that reads

Warning: Cannot modify header information - headers already sent by (output started at /home/pmmotoco/public_html/services.php:8) in /home/pmmotoco/public_html/incs/sendmail.inc on line 36

When in doubt, go flat out!

 
a search would have returned hundreds of results.

you cannot send any html (including whitespace) to the browser prior to calling the header function.

*cLFlaVA
----------------------------
[tt]a frickin' twelve-gauge, what do you think?[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
This is where having the code that displays the form, checks the form for errors, and processes the correct information in one source file comes in very handy. You don't have to redirect anywhere.

Here is the basic flow (pseudo-code, some real PHP) that I use:
Code:
<?php
$err_msgs = array();
if (isset($_POST['submit'])) {
   foreach($_POST as $k=>$v)
      switch ($k) {
        case 'input_field_1': // here's where you do your checking.... you can list more than one field here, one per case statement
            if (trim($v) == '')
                $err_msgs[] = 'Field ' . $k . ' can not be left blank';
            break;
        case 'numeric_field': // just as an example
            if (!is_numeric($v))
                $err_msgs[] = ' Field ' . $k . ' is not all numeric';
            break;
      } // end switch
   if (!empty($err_msgs)) unset($_POST['submit']); 
if (empty($err_msgs)) {
   // do your ok processing here
}
if (!isset($_POST['submit'])) {
//
//
//  display form (and errors if any). Make sure to redisplay and values entered if any
//
//
   if (!empty($err_msgs))
      echo implode("\n",$err_msgs)."\n" // display the error messages

// display your form here, make sure that the action points to $_SERVER['PHP_SELF']

}
?>
Note: I just typed this in without really proofing it for typos, but the logic is there.

Ken
 
Ah, as this page is an include it there was all kinds of html being sent before it was included. I was looking at the include and thinking that that was the whole page, oops!

When in doubt, go flat out!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top