Forum Moderators: coopster

Message Too Old, No Replies

getting error when using an if statement in my form posting

         

anzagi

12:38 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



hi, I am using the following code to post my form, now i'm sure this was working before, but now when I submit the form but leave fields blank to test the validation, the form submits anyway, but I get the error page (fields are blank page) and get this message:

"Warning: Cannot modify header information - headers already sent by (output started at forms/confirmation.php:27) in forms/confirmation.php on line 188"

it like it is still processing the code even though its in an if statement?

can anyone help please?

CODE:

<?php
$name = $_REQUEST['name'] ;
$company = $_REQUEST['company'] ;
$email = $_REQUEST['email'] ;
$phone = $_REQUEST['phone'] ;
$comments = $_REQUEST['comments'] ;
$subject = $_REQUEST['subject'] ;
$page = $_REQUEST['page'] ;
$message = "From:".$name;
$message .= "\nCompany:".$company;
$message .= "\nEmail:".$email;
$message .= "\nPhone Number:".$phone;
$message .= "\nComments:".$comments;
//checks to see if the name field has been defined (incase someone has gone to the process page directly, if not it redirects back to the form //
if (!isset($_REQUEST['name'])) {
header( "Location: http://www.example.com/forms/index.php" );
//checks if the user has submitted the form with the specified fields left blank//
}
elseif (empty($name) ¦¦ empty($company) ¦¦ empty($email)) {
?>

<?php
}
//if all is well the form is then submitted//

if ( $subject == "Please keep me informed of future seminars" ) {
mail( "enquiries@example.com", "$subject", $message );
header( "Location: http://www.example.com/forms/seminar/thankyou.php" );
}
else {
mail( "enquiries@example.com", "$subject", $message );
header( "Location: http://www.example.com/forms/thankyou.php?page=$page" );
}
?>

[edited by: eelixduppy at 4:21 pm (utc) on April 29, 2008]
[edit reason] exemplified [/edit]

RonPK

2:39 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



elseif (empty($name) ¦¦ empty($company) ¦¦ empty($email)) {
?>

<?php
}

You're dropping out of PHP, which causes the web server to send data (just a newline here) to the browser. You cannot use a function like header() after that.

d40sithui

2:41 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



that just means you got some output before you call header().
remember header only works if there is absolutely no output before it is called. This line seems like suspect. Why did you end php and start it back again? the blank line might have account for the output that makes header() return an error.


elseif (empty($name) ¦¦ empty($company) ¦¦ empty($email)) {
?>

<?php
}

henry0

2:42 pm on Apr 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A couple of things:
Try not using $_REQUEST as "one size fits all"
unless you are going back and forth from page a to page b and using a mix of both POST and GET
which could look like spaghetti code :)

When testing for a condition
if the condition is not what is expected
end it by using exit:

{
// if this or that etc..
echo"go back to ...";
exit();
// exit stop exec
}

<edit> regarding header: you may use it from any script location if OB is turned on in your php.ini</edit>

anzagi

2:55 pm on Apr 29, 2008 (gmt 0)

10+ Year Member



cheers for the responses all.