473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

header() with if/then statements

I have created a verification script to verify information and redirect
the customer to the appropriate error page. For example:

if ($FName=""){
header('Location:/verify_fname.htm');
}
else{
if ($LName=""){
header('Location:/verify_lname.htm');
}
else{
if ($Company=""){
header('Location:/verify_company.htm');
}
else{
if ($Title=""){
header('Location:/verify_title.htm');
}
}
}
}

The intent of the code is to check a variable. If an error is found in
the variable, it redirects to the correct error page. Otherwise, it
continues on through the else statement which then checks another
variable and so and so on. The final else statement redirects to a
process script that takes all the information writes it to a table. I
am getting this error message:
Cannot modify header information - headers already sent by

I can't put the redirect before the HTML as I want the redirect to be
conditional. Any help would be appreciated.

Nov 17 '06 #1
12 1855
Jerim79 wrote:
I have created a verification script to verify information and redirect
the customer to the appropriate error page. For example:

if ($FName=""){
header('Location:/verify_fname.htm');
}
else{
if ($LName=""){
header('Location:/verify_lname.htm');
}
else{
if ($Company=""){
header('Location:/verify_company.htm');
}
else{
if ($Title=""){
header('Location:/verify_title.htm');
}
}
}
}

The intent of the code is to check a variable. If an error is found in
the variable, it redirects to the correct error page. Otherwise, it
continues on through the else statement which then checks another
variable and so and so on. The final else statement redirects to a
process script that takes all the information writes it to a table. I
am getting this error message:
Cannot modify header information - headers already sent by

I can't put the redirect before the HTML as I want the redirect to be
conditional. Any help would be appreciated.
Jerim,

Why don't you want the redirect before the HTML? If you redirect the
user, you don't want the HTML to show, do you?

It can still be conditional. Put it at the top, and if everything is
OK, just have it fall through to the HTML.

BTW - this is a very user-unfriendly way of doing it. You should rather
check all the options, then if any are incorrect, redirect with all of
the invalid information. This way if there are three things wrong, the
user must gets an error message for the first one and corrects it. Then
he gets an error message for the second one, and so on.

If you check them all, you can display all three error messages at the
same time. Much more user friendly.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 17 '06 #2
It can still be conditional. Put it at the top, and if everything is
OK, just have it fall through to the HTML.

BTW - this is a very user-unfriendly way of doing it. You should rather
check all the options, then if any are incorrect, redirect with all of
the invalid information. This way if there are three things wrong, the
user must gets an error message for the first one and corrects it. Then
he gets an error message for the second one, and so on.

If you check them all, you can display all three error messages at the
same time. Much more user friendly.
Something like this, I believe:

<?php

// Code to check validity of data.

if (everything valid) {
// code to process data
// redirect to new page
} else {
// Set error messages for individual data elements.
}

?>

<html>

<form>
<input 1>
<?php if ($errorMsg1 != "") print $errorMsg1 ?>
<input 2>
<?php if ($errorMsg2 != "") print $errorMsg2 ?>
...

</html>

Nov 17 '06 #3

e_*******@hotmail.com wrote:
It can still be conditional. Put it at the top, and if everything is
OK, just have it fall through to the HTML.

BTW - this is a very user-unfriendly way of doing it. You should rather
check all the options, then if any are incorrect, redirect with all of
the invalid information. This way if there are three things wrong, the
user must gets an error message for the first one and corrects it. Then
he gets an error message for the second one, and so on.

If you check them all, you can display all three error messages at the
same time. Much more user friendly.

Something like this, I believe:

<?php

// Code to check validity of data.

if (everything valid) {
// code to process data
// redirect to new page
} else {
// Set error messages for individual data elements.
}

?>

<html>

<form>
<input 1>
<?php if ($errorMsg1 != "") print $errorMsg1 ?>
<input 2>
<?php if ($errorMsg2 != "") print $errorMsg2 ?>
...

</html>
I appreciate the help. I think the concept is getting clearer. I am
just curious though. The first time a person loads the webpage wouldn't
the php execute and since all of the variables are null, wouldn't it
automatically display an error message even before the person has had a
chance to fill out the form?

Nov 17 '06 #4
Jerim79 wrote:
e_*******@hotmail.com wrote:
>>>It can still be conditional. Put it at the top, and if everything is
OK, just have it fall through to the HTML.

BTW - this is a very user-unfriendly way of doing it. You should rather
check all the options, then if any are incorrect, redirect with all of
the invalid information. This way if there are three things wrong, the
user must gets an error message for the first one and corrects it. Then
he gets an error message for the second one, and so on.

If you check them all, you can display all three error messages at the
same time. Much more user friendly.

Something like this, I believe:

<?php

// Code to check validity of data.

if (everything valid) {
// code to process data
// redirect to new page
} else {
// Set error messages for individual data elements.
}

?>

<html>

<form>
<input 1>
<?php if ($errorMsg1 != "") print $errorMsg1 ?>
<input 2>
<?php if ($errorMsg2 != "") print $errorMsg2 ?>
...

</html>


I appreciate the help. I think the concept is getting clearer. I am
just curious though. The first time a person loads the webpage wouldn't
the php execute and since all of the variables are null, wouldn't it
automatically display an error message even before the person has had a
chance to fill out the form?
If you're validating on the same page, yes. You have to test for that.

For instance, if you have your submit button as:

<input type="submit" name="submit" value="Submit">

you could check:

if (isset[$_POST['submit'] && $_POST['submit'] == 'Submit') {
... do your validation here

Just be sure no other page posts to this one with the same submit button.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 17 '06 #5
I appreciate the help. I think the concept is getting clearer. I am
just curious though. The first time a person loads the webpage wouldn't
the php execute and since all of the variables are null, wouldn't it
automatically display an error message even before the person has had a
chance to fill out the form?
The structure of my pages looks like this:

<?php // Very first thing, so header redirects work

// Initialize variables that appear in html, so they are empty, not
null

if ($_POST) {
// validate & process data
}

?>

<html>
web content
</html>
The if ($_POST) prevents any validation from being done until the user
has submitted some data. It is common for many people not to bother
initializing the variables, but if you look at your html output, it's
pretty ugly with a bunch of "uninitialized variable" messages in the
code. This all works for me locally, but I haven't gone live with it
yet. Am I missing anything?

Nov 17 '06 #6

Jerry Stuckle wrote:
Jerim79 wrote:
e_*******@hotmail.com wrote:
>>It can still be conditional. Put it at the top, and if everything is
OK, just have it fall through to the HTML.

BTW - this is a very user-unfriendly way of doing it. You should rather
check all the options, then if any are incorrect, redirect with all of
the invalid information. This way if there are three things wrong, the
user must gets an error message for the first one and corrects it. Then
he gets an error message for the second one, and so on.

If you check them all, you can display all three error messages at the
same time. Much more user friendly.

Something like this, I believe:

<?php

// Code to check validity of data.

if (everything valid) {
// code to process data
// redirect to new page
} else {
// Set error messages for individual data elements.
}

?>

<html>

<form>
<input 1>
<?php if ($errorMsg1 != "") print $errorMsg1 ?>
<input 2>
<?php if ($errorMsg2 != "") print $errorMsg2 ?>
...

</html>

I appreciate the help. I think the concept is getting clearer. I am
just curious though. The first time a person loads the webpage wouldn't
the php execute and since all of the variables are null, wouldn't it
automatically display an error message even before the person has had a
chance to fill out the form?

If you're validating on the same page, yes. You have to test for that.

For instance, if you have your submit button as:

<input type="submit" name="submit" value="Submit">

you could check:

if (isset[$_POST['submit'] && $_POST['submit'] == 'Submit') {
... do your validation here

Just be sure no other page posts to this one with the same submit button.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Here is the basic gist of what I have:

<?php
$FName=$_Post['FName'];

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>

<html>
<body>
Registration
<?php if ($errormsg1 !="") echo $errormsg1); ?>
<br />
<form action="(this page)" method="Post">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit value="Submit">
</form>
</body>
</html>

Forgetting the problem about loading the errors up before filling out
the form, I can't get this to work at all. After I click on submit it
just brings me back to the same page, with no error messages.

Nov 17 '06 #7
Here is the basic gist of what I have:

<?php
$FName=$_Post['FName'];

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>

<html>
<body>
Registration
<?php if ($errormsg1 !="") echo $errormsg1); ?>
<br />
<form action="(this page)" method="Post">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit value="Submit">
</form>
</body>
</html>

Forgetting the problem about loading the errors up before filling out
the form, I can't get this to work at all. After I click on submit it
just brings me back to the same page, with no error messages.
Try:
<?php

$FName = "";
$errorMsg1 = "";

if ($_POST) {

if ( isset($_POST['FName']) ) {
$FName = $_POST['FName'];
}

Now you can validate $FName, because it's either empty or set to
user's value. Don't write to database without validating all data.
The rest looks good. Good luck.

Nov 17 '06 #8

e_matt...@hotmail.com wrote:
Here is the basic gist of what I have:

<?php
$FName=$_Post['FName'];

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>

<html>
<body>
Registration
<?php if ($errormsg1 !="") echo $errormsg1); ?>
<br />
<form action="(this page)"
method="Post">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit value="Submit">
</form>
</body>
</html>

Forgetting the problem about loading the errors up before filling out
the form, I can't get this to work at all. After I click on submit it
just brings me back to the same page, with no error messages.

Try:
<?php

$FName = "";
$errorMsg1 = "";

if ($_POST) {

if ( isset($_POST['FName']) ) {
$FName = $_POST['FName'];
}

Now you can validate $FName, because it's either empty or set to
user's value. Don't write to database without validating all data.
The rest looks good. Good luck.
Since I am still learning, I just wanted state the logic of the code.
First we set $FName equal to nothing. Then we set $errorMsg1 equal to
nothing. Next we say "if the method is $_POST" then check to see if
$_POST['FName'] has been set. If it has, then set $FName equal to
$_POST['FName']

I still can't get it to display the error message.

Nov 17 '06 #9

Jerim79 wrote:
e_matt...@hotmail.com wrote:
Here is the basic gist of what I have:
>
<?php
$FName=$_Post['FName'];
>
if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>
>
<html>
<body>
Registration
<?php if ($errormsg1 !="") echo $errormsg1); ?>
<br />
<form action="(this page)"
method="Post">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit value="Submit">
</form>
</body>
</html>
>
Forgetting the problem about loading the errors up before filling out
the form, I can't get this to work at all. After I click on submit it
just brings me back to the same page, with no error messages.
Try:
<?php

$FName = "";
$errorMsg1 = "";

if ($_POST) {

if ( isset($_POST['FName']) ) {
$FName = $_POST['FName'];
}

Now you can validate $FName, because it's either empty or set to
user's value. Don't write to database without validating all data.
The rest looks good. Good luck.

Since I am still learning, I just wanted state the logic of the code.
First we set $FName equal to nothing. Then we set $errorMsg1 equal to
nothing. Next we say "if the method is $_POST" then check to see if
$_POST['FName'] has been set. If it has, then set $FName equal to
$_POST['FName']

I still can't get it to display the error message.
Here is the current, non-working code:

<?php
$FName="";
$errormsg1="";

if( isset($_POST['FName']){
$FName=$_POST['FName'];
}

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>
<html>
<body>
Registration
<?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
<form action="new.htm" method="POST">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

Nov 17 '06 #10
Jerim79 wrote:
Here is the current, non-working code:

<?php
$FName="";
$errormsg1="";

if( isset($_POST['FName']){
$FName=$_POST['FName'];
}

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>
<html>
<body>
Registration
<?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
<form action="new.htm" method="POST">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Here's how I'd do it:

<?php
########
# initialize variables for user input and error messages
$FName = false;
$FName_error = false;
$LName = false;
$LName_error = false;
# initialize an error count variable
$error_count = 0;

########
# Check if the page was accessed with a POST
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$FName = isset($_POST['FName']) ? $_POST['FName'] : '';
if (!name_is_valid($FName)) {
$FName_error = 'Please enter a valid First Name.';
++$error_count;
}
$LName = isset($_POST['LName']) ? $_POST['LName'] : '';
if (!name_is_valid($LName)) {
$LName_error = 'Please enter a valid Last Name.';
++$error_count;
}
if (!$error_count) {
########
# If the script reaches here, there have been no errors detected in
# the user data, so we can save to the database, send mail, display
# a 'thank you' page, and/or redirect
// mysql_query("insert ...");
// exit('Thank you');
}
}

########
# When we get here after a GET $error_count will be 0 (zero).
# When we get here after a POST $error_count will *NOT* be 0
# because in the if (!$error_count) above we always exit().
# So
if ($error_count) {
echo 'There were errors in your submission. Please review and re-submit';
}

echo '<form action="', $_SERVER['PHP_SELF], '" method="post">';
echo 'First Name: <input type="text" value="', $FName, '">';
if ($FName_error) {
echo ' <span class="error">', $FName_error, '</span>';
}
echo "<br>\n";

echo 'Last Name: <input type="text" value="', $LName, '">';
if ($LName_error) {
echo ' <span class="error">', $LName_error, '</span>';
}
echo "<br>\n";

echo '<input type="submit">';
echo '</form>';
HTML;


Happy Coding :)

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 17 '06 #11
Jerim79 wrote:
Jerim79 wrote:
>>e_matt...@hotmail.com wrote:
>>>>Here is the basic gist of what I have:

<?php
$FName=$_Post['FName'];

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>

<html>
<body>
Registration
<?php if ($errormsg1 !="") echo $errormsg1); ?>
<br />
<form action="(this page)"

method="Post">
>>>>First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit value="Submit">
</form>
</body>
</html>

Forgetting the problem about loading the errors up before filling out
the form, I can't get this to work at all. After I click on submit it
just brings me back to the same page, with no error messages.

Try:
<?php

$FName = "";
$errorMsg1 = "";

if ($_POST) {

if ( isset($_POST['FName']) ) {
$FName = $_POST['FName'];
}

Now you can validate $FName, because it's either empty or set to
user's value. Don't write to database without validating all data.
The rest looks good. Good luck.

Since I am still learning, I just wanted state the logic of the code.
First we set $FName equal to nothing. Then we set $errorMsg1 equal to
nothing. Next we say "if the method is $_POST" then check to see if
$_POST['FName'] has been set. If it has, then set $FName equal to
$_POST['FName']

I still can't get it to display the error message.


Here is the current, non-working code:

<?php
$FName="";
$errormsg1="";

if( isset($_POST['FName']){
$FName=$_POST['FName'];
}

if ($FName !=""){
header("Location: write_to_database.php");
}
else{
if ($FName=""){
$errormsg1="Please enter a first name":
}
?>
<html>
<body>
Registration
<?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
<form action="new.htm" method="POST">
First Name: <input type="text" name="FName">
<br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
I do things a little differently, but basically the same thing. This
should work. Note that the <?php MUST be the first thing in you file -
no whitespace or anything before it.

<?php

$errormsg1="";
$FName = "";
if (isset($_POST['submit'] && $_POST['submit'] == 'Submit') {

// Repeat the following for each field you wish to validate

if (isset($_POST['FName']))
$FName = trim($_POST['FName'];
if ($FName == "")
$errormsg1 = "Please enter a first name<br>\n";

// End of repeated code

if ($errormsg1 == "")
header("Location: write_to_database.php");
}
?>
<html>
<body>
Registration
<?php if ($errormsg1 !=""){ echo $errormsg1; } ?>
<form action="new.htm" method="POST">
First Name: <input type="text" name="FName" value="<?$FName?>">
<br />
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>

The first if checks to see if the submit button was pressed.

The next if checks to see if FName was posted to the page. If so, it
trims white space before and after (in case the user entered a blank
space) and sets it into $FName.

The next if checks to see if FName is an empty string. If so, it sets
errormsg1.

You can repeat these statements to validate other fields.

Now - if errormsg1 is empty, it will call your write_to_database page
(more problems here - in a minute).

If there was an error, or the submit button was not pressed, it will
fall through to the rest of your code.

Finally, I added code to your input field to echo $FName. This will
fill in the field if your user has entered a first name but may be
missing other data. Otherwise your form will come up blank and your
user will have to reenter everything.

Now - the problem with your code. When you call the header() function
to transfer control, the data in $_POST is NOT sent with it. You will
lose all of the data that was posted to your page. Rather than
transferring control, you should write to the database here in this
page, i.e.

if ($errormsg1 == "") {
// code to connect to and write to your database
header('Location: thankyou.html');
}

Where 'thankyou.html' is a page that gives them a message thanking them
for their input. At least I'm assuming you don't wish to return to this
form. If so, you can leave out the header call. And if you don't want
the form to show the $FName again, just set it to "" again at this
location.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Nov 17 '06 #12
>I have created a verification script to verify information and redirect
>the customer to the appropriate error page. For example:

if ($FName=""){
header('Location:/verify_fname.htm');
}
else{
if ($LName=""){
header('Location:/verify_lname.htm');
}
else{
if ($Company=""){
header('Location:/verify_company.htm');
}
else{
if ($Title=""){
header('Location:/verify_title.htm');
}
}
}
}

The intent of the code is to check a variable. If an error is found in
the variable, it redirects to the correct error page. Otherwise, it
continues on through the else statement which then checks another
variable and so and so on. The final else statement redirects to a
process script that takes all the information writes it to a table. I
am getting this error message:
Cannot modify header information - headers already sent by

I can't put the redirect before the HTML as I want the redirect to be
conditional. Any help would be appreciated.
You MUST put the redirect before outputting any HTML, and outputting HTML
after the redirect is somewhat pointless, as it won't be seen.

Your code doesn't contain any HTML, though. Code within <?php ... ?>
isn't output, and won't draw that error message. However, stuff outside
that, like blank lines, DOCTYPE, HTML, UTF-8 markers, etc. are and
will mess things up.

Nov 17 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Charles Soto | last post by:
I've got a main loop script that calls two other scripts that do no user interaction. All they do is send a couple of mysql update statements. Then they use header() to call the main loop again. ...
6
by: Lochness | last post by:
I'm hoping someone can help me with this. I've seen and tried various solutions I've seen on the net, but nothing works. Of course it works perfectly on localhost, but when I upload it to the...
7
by: beliavsky | last post by:
Ideally, one can use someone's C++ code by just looking at the header files (which should contain comments describing the functions in addition to function definitions), without access to the full...
2
by: puzzlecracker | last post by:
I heard different school of thoughts but most of the seem to agreet that it is a bad idea to include using namaspace std; in header files, even though it is non-issue (for the most part - unless...
3
by: Pietro Cerutti | last post by:
Hi Group, suppose test1.c, test2.c and test.h /*** BEGIN TEST.H ***/ #ifndef _TEST_H #define _TEST_H typedef struct {
2
by: runway27 | last post by:
i have a question about using header("Location: filename.html"); in php with my present code i have a few echo statements followed by header("Location: filename.html"); because i have echo ...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
4
by: JRough | last post by:
I have this section at the end of a page ------------------- if ($_POST== 'Open in Excel'){ if (empty($data)) { $data = "\n(0) Records Found!\n";} header("Content-type:...
1
by: WT | last post by:
Hello, My web application uses themes, selecting a theme from codebind for current page object. Each theme includes several css files. And I also add some specific javascript from codebehind as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.