| View previous topic :: View next topic | | Author | Message |
|---|
Jerim79 Guest
| Posted: Fri Nov 17, 2006 1:08 pm Post subject: 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. |
| | Back to top | |  | Jerry Stuckle Guest
| Posted: Fri Nov 17, 2006 1:31 pm Post subject: Re: header() with if/then statements |
| Jerim79 wrote:
| Quote: | 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. jstucklex@attglobal.net ================== |
| | Back to top | |  | Guest
| Posted: Fri Nov 17, 2006 3:06 pm Post subject: Re: header() with if/then statements |
| | Quote: | 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> |
| | Back to top | |  | Jerim79 Guest
| Posted: Fri Nov 17, 2006 3:55 pm Post subject: Re: header() with if/then statements |
| e_matthes@hotmail.com wrote:
| Quote: | 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? |
| | Back to top | |  | Jerry Stuckle Guest
| Posted: Fri Nov 17, 2006 3:59 pm Post subject: Re: header() with if/then statements |
| Jerim79 wrote:
| Quote: | e_matthes@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. jstucklex@attglobal.net ================== |
| | Back to top | |  | Guest
| Posted: Fri Nov 17, 2006 4:07 pm Post subject: Re: header() with if/then statements |
| | Quote: | 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? |
| | Back to top | |  | Jerim79 Guest
| Posted: Fri Nov 17, 2006 4:17 pm Post subject: Re: header() with if/then statements |
| Jerry Stuckle wrote:
| Quote: | Jerim79 wrote: e_matthes@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. jstucklex@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. |
| | Back to top | |  | Guest
| Posted: Fri Nov 17, 2006 4:36 pm Post subject: Re: header() with if/then statements |
| | Quote: | 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. |
| | Back to top | |  | Jerim79 Guest
| Posted: Fri Nov 17, 2006 4:49 pm Post subject: Re: header() with if/then statements |
| e_matt...@hotmail.com wrote:
| Quote: | 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. |
| | Back to top | |  | Jerim79 Guest
| Posted: Fri Nov 17, 2006 5:15 pm Post subject: Re: header() with if/then statements |
| Jerim79 wrote:
| Quote: | 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> |
| | Back to top | |  | Pedro Graca Guest
| Posted: Fri Nov 17, 2006 6:17 pm Post subject: Re: header() with if/then statements |
| Jerim79 wrote:
| Quote: | 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*. |
| | Back to top | |  | Jerry Stuckle Guest
| Posted: Fri Nov 17, 2006 6:36 pm Post subject: Re: header() with if/then statements |
| Jerim79 wrote:
| Quote: | 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. jstucklex@attglobal.net ================== |
| | Back to top | |  | |
|