...

View Full Version : Multiple Form Processing



cyclist-Don
05-12-2008, 10:45 PM
I am developing a web site where there are 4 pages of form information the user needs to fill out. I want the user to go through the first page form, press process, the form gets processed and if no errors, displays the next form. I looked everywhere for answers and haven't found a solution. The code is like what is below.

Everything works fine on the first form, data is put in the db, everything is fine except for displaying the second form. The "header" command gives the following warning:
Warning: Cannot modify header information - headers already sent by (output started at C:\Inetpub\vhosts\url.com\httpdocs\Test\newclient.php:3) in C:\Inetpub\vhosts\url.com\httpdocs\Test\newclient.php on line 29

Line 29 is the header command line.

Any suggestions or alternatives:confused: would be very helpful and very grateful! Thanks in advance!
cyclist-don

Below is the code...


<?php
if (!isset($_POST['firstname'])){
?>

<form name="thisform" action="newclient.php" method="POST">
First Name: <input type="text" name="firstname" maxlength="30" size="30"><br>
Last Name: <input type="text" name="lastname" maxlength="30" size="30"><br>
<input type="submit" value="Submit Information" name="Submit"></TD>
</form>

<?php
}else{

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

//database connection is here

$query = "INSERT IGNORE INTO client_info
SET firstname = '$firstname',
lastname = '$lastname'";
@mysql_query($query);
if (mysql_errno() == 0) {
header("Location: newclient-details.php");
} else {
echo "**** unable to add Client Info, error = ".mysql_errno(),"<br>";
}
}
?>

mlseim
05-12-2008, 11:41 PM
Without testing this ...
Try this:



<?php
if (!isset($_POST['firstname'])){
echo"
<form name='thisform' action='newclient.php' method='POST'>
First Name: <input type='text' name='firstname' maxlength='30' size='30'><br>
Last Name: <input type='text' name='lastname' maxlength='30' size='30'><br>
<input type='submit' value='Submit Information' name='Submit'></TD>
</form>
";

}else{

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

//database connection is here

$query = "INSERT IGNORE INTO client_info
SET firstname = '$firstname',
lastname = '$lastname'";
@mysql_query($query);
if (mysql_errno() == 0) {
header("Location: newclient-details.php");
} else {
echo "**** unable to add Client Info, error = ".mysql_errno(),"<br>";
}
}
?>



I'm thinking that by leaving PHP, doing some HTML and coming back to PHP,
it's putting data into the HTML header, thus causing the header error.
Just a shot in the dark...

cyclist-Don
05-12-2008, 11:54 PM
Tried your suggestion. I am still getting the same warning. Also, I forgot to mention that I tried suppressing the warnings and only a blank screen appeared. Thanks for the suggestion!

cyclist-don

Mwnciau
05-13-2008, 01:45 AM
Is there ANY output before the header command? Even a newline or space can cause this error message.

Alternatively you could just use a meta redirect:

echo '<meta http-equiv="refresh" content="0;url=nextpage.php" /><a href="nextpage.php">Click here if you are not redirected automatically</a>';

cyclist-Don
05-13-2008, 02:17 AM
I doubled checked and there were no spaces or any data being displayed.

The redirect worked fine, thanks!

Would you recommend a different way of doing what I am trying to achieve?

Thanks for all your help!

mlseim
05-13-2008, 04:06 AM
Another shot at it ...

Try this, with an "exit" inserted:



<?php
if (!isset($_POST['firstname'])){
echo"
<form name='thisform' action='newclient.php' method='POST'>
First Name: <input type='text' name='firstname' maxlength='30' size='30'><br>
Last Name: <input type='text' name='lastname' maxlength='30' size='30'><br>
<input type='submit' value='Submit Information' name='Submit'></TD>
</form>
";

}else{

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];

//database connection is here

$query = "INSERT IGNORE INTO client_info
SET firstname = '$firstname',
lastname = '$lastname'";
@mysql_query($query);
if (mysql_errno() == 0) {
header("Location: newclient-details.php");
exit;
} else {
echo "**** unable to add Client Info, error = ".mysql_errno(),"<br>";
}
}
?>

cyclist-Don
05-13-2008, 05:02 AM
I added the exit in after the header statement, as you had shown. The error message was the same.

I am wondering if there may be a better method to do this or stick with the meta redirect. Any help or suggestions would be greatly appreciated.

Thanks for all your help!

aedrin
05-13-2008, 02:59 PM
I tried suppressing the warnings and only a blank screen appeared.

It's a bad idea to just suppress warnings, but when you get a blank screen it means you have a syntax error that prevents the script from even starting.

RMcLeod
05-13-2008, 03:11 PM
When I have a large form, instead of splitting it over several pages I have it submit to itself and then decide whereabouts in the process we are, if there were any errors in validation and therefore what part of the form to display.

e.g.


<?php
if(!$_POST || ($_POST['form'] == 'first_part' && $err == 1)) {
?>
<form action="page.php" method="post">
<input type="hidden" name="form" value="first_part" />
...
</form>
<?php } elseif($_POST['form'] == 'first_part' || ($_POST == 'second_part' && $err == 1)) { ?>
<form action="page.php" method="post">
<input type="hidden" name="form" value="second_part" />
...
</form>
<?php } ?>

cyclist-Don
05-13-2008, 03:12 PM
I agree with you about suppressing warnings. The syntax for the header statement is as shown in previous posts. Also, when I changed from the header command to the meta redirect, everything worked fine.

I am just trying to find a different solution so I didn't have to use the meta redirect.

Thanks for the help!

RMcLeod
05-13-2008, 03:14 PM
You might not have seen my post ^^^ as we both submitted a reply at more or less the same time.

cyclist-Don
05-13-2008, 03:36 PM
Thanks for the post regarding one program to process all of the 4 forms of data. This is a good option as well. Over a large amount of data and forms, this could get overwhelming for 1 program. Thanks for the recommendation!

RMcLeod
05-13-2008, 03:56 PM
Thanks for the post regarding one program to process all of the 4 forms of data. This is a good option as well. Over a large amount of data and forms, this could get overwhelming for 1 program. Thanks for the recommendation!

Not overwhelming at all, as the form is processed bit by bit. Have a seperate script that does all the validation and processing. Use a similar elseif statement block to determine which part of the form has been submitted and requires processing.

denizenkane
05-13-2008, 03:57 PM
You could try output buffering. Lookup ob_start() and ob_end_flush() in the manual.

cyclist-Don
05-13-2008, 07:00 PM
Great ideas and options. Thanks to all. I will be trying all the options. At least now I have options. Thanks again for the help!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum