473,587 Members | 2,548 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('Locatio n:/verify_fname.ht m');
}
else{
if ($LName=""){
header('Locatio n:/verify_lname.ht m');
}
else{
if ($Company=""){
header('Locatio n:/verify_company. htm');
}
else{
if ($Title=""){
header('Locatio n:/verify_title.ht m');
}
}
}
}

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 1884
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('Locatio n:/verify_fname.ht m');
}
else{
if ($LName=""){
header('Locatio n:/verify_lname.ht m');
}
else{
if ($Company=""){
header('Locatio n:/verify_company. htm');
}
else{
if ($Title=""){
header('Locatio n:/verify_title.ht m');
}
}
}
}

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*******@attgl obal.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_*******@hotma il.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_*******@hotma il.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*******@attgl obal.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 "uninitiali zed 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_*******@hotma il.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*******@attgl obal.net
=============== ===
Here is the basic gist of what I have:

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

if ($FName !=""){
header("Locatio n: write_to_databa se.php");
}
else{
if ($FName=""){
$errormsg1="Ple ase 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("Locatio n: write_to_databa se.php");
}
else{
if ($FName=""){
$errormsg1="Ple ase 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...@hotma il.com wrote:
Here is the basic gist of what I have:

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

if ($FName !=""){
header("Locatio n: write_to_databa se.php");
}
else{
if ($FName=""){
$errormsg1="Ple ase 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...@hotma il.com wrote:
Here is the basic gist of what I have:
>
<?php
$FName=$_Post['FName'];
>
if ($FName !=""){
header("Locatio n: write_to_databa se.php");
}
else{
if ($FName=""){
$errormsg1="Ple ase 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("Locatio n: write_to_databa se.php");
}
else{
if ($FName=""){
$errormsg1="Ple ase 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

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

Similar topics

1
2287
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. This works fine, EXCEPT the mysql statement isn't finished by the time the main loop is called. This is a problem because the main loop actually...
6
3393
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 server (1and1.com) it gives errors. The user enters a code, the code is verified, a new one is created and it's supposed to send them to the next...
7
25571
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 source code. Can analogs of C++ header files be created for Python code? Python "header" files could list only the 'def' statements and...
2
1797
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 someone purposely created,for example, cout type for some crazy reason). Anyone one would argue about cluause using and how should it be used and...
3
2064
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
1589
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 statements before header("Location: filename.html"); i am getting an error that headers have already been sent. i cannot remove the echo statements...
10
5945
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 is unused, I know that it does not take up storage in the
4
2542
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: application/xmsdownload"); header("Content-Disposition: attachment; filename=". $file_name.date("Y:m:d H:i").".xls"); header("Pragma: no-cache");
1
1731
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 links in the header object. Using VS2008, framework 3.5, C#. I need to set conditional statements in the header section of page to
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8215
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.