By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,677 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,677 IT Pros & Developers. It's quick & easy.

How to redirect after form submit?

P: n/a
I have a basic feedback form with a submit button.

After the "send" button is clicked, I want the user to be redirected to a
different page that says "Your message has been sent."

How do I do this?

I've tried using:

header("Location:http://www.mysite.com/send-confirm.php");

But here's the error I get:

Warning: Cannot add header information - headers already sent by (output started
at ...

Here's the code:

<form action= "
<?php
$message = trim($_REQUEST['message']);
$email_address = trim($_REQUEST['email_address']);
if (empty($message) || empty($email_address))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
?>"
<input name="email_address" type="text" size="30"/><br />
<textarea name="message" rows="12" cols="23"></textarea><br />
<input type="submit" value="Send">
</form>
<?php
if ($valid_message)
{
echo "valid message";
header("Location:http://www.mysite.com/send-confirm.php");
exit();
}
else
{
echo "invalid message";
}
?>

How do I redirect after the user clicks the send button?

Thanks in advance.
Jun 12 '06 #1
Share this Question
Share on Google+
13 Replies


P: n/a
deko wrote:
I have a basic feedback form with a submit button.

After the "send" button is clicked, I want the user to be redirected
to a different page that says "Your message has been sent."

How do I do this?

I've tried using:

header("Location:http://www.mysite.com/send-confirm.php");

But here's the error I get:

Warning: Cannot add header information - headers already sent by
(output started at ...

Here's the code:

<form action= "
<?php
$message = trim($_REQUEST['message']);
$email_address = trim($_REQUEST['email_address']);
if (empty($message) || empty($email_address))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
"

<input name="email_address" type="text" size="30"/><br />
<textarea name="message" rows="12" cols="23"></textarea><br />
<input type="submit" value="Send">
</form>
<?php
if ($valid_message)
{
echo "valid message";
header("Location:http://www.mysite.com/send-confirm.php");
exit();
}
else
{
echo "invalid message";
}


How do I redirect after the user clicks the send button?

Thanks in advance.


Put all the stuff that does output ("<form...", "<input...") after the
header() call.
Jun 12 '06 #2

P: n/a
> Put all the stuff that does output ("<form...", "<input...") after the
header() call.


Okay, here's the header call:

<?php if ($valid_message)
{
header("Location:http://www.mysite.com/send-confirm.php");
exit();
}
?>

And here's the form:

<form action="
<?php
$message = trim($_REQUEST['message']);
$email_address = trim($_REQUEST['email_address']);
if (empty($message) || empty($email_address))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
?>" method="POST" name="feedback" style="font-size:12px" >
<input name="email_address" type="text" size="30"/><br />
<textarea name="message" rows="12" cols="23"></textarea><br />
<input type="submit" value="Send">
</form>

I no longer get the error, but the page does not redirect.

I'm sure I must be missing something simple...
Jun 12 '06 #3

P: n/a
I tried this:

<?php

$valid_message = true;

if ($valid_message)
{
header("Location:http://bubba/send-confirm.php");
exit();
}
?>

And still got this:

Warning: Cannot add header information - headers already sent by (output started
....

Can I redirect another way?

The headers, probably, are being sent in the header section of the page.

Can this block of code be put above the DOCTYPE statement?

But then, how would the value of the $valid_message variable get set?
Jun 12 '06 #4

P: n/a
You must ensure that there is no blank space or empty lines, outside of the
<?php ?> braces. Any such empty space will be sent and will cause headers to
already be sent.

Make sure that <?php is the very first thing on the very first line of the
file.

deko wrote:
I tried this:

<?php

$valid_message = true;

if ($valid_message)
{
header("Location:http://bubba/send-confirm.php");
exit();
}


And still got this:

Warning: Cannot add header information - headers already sent by
(output started ...

Can I redirect another way?

The headers, probably, are being sent in the header section of the
page.
Can this block of code be put above the DOCTYPE statement?

But then, how would the value of the $valid_message variable get set?


Jun 12 '06 #5

P: n/a
> You must ensure that there is no blank space or empty lines, outside of the
<?php ?> braces. Any such empty space will be sent and will cause headers to
already be sent.

Make sure that <?php is the very first thing on the very first line of the
file.


but what about all the html?

the page looks like this:

DOCTYPE ...
<html>
<head>
....
javascript...
</head>
<body>
<html>
all kinds of html...
<form>
<?php
....
?>
....
</form>
more html...
</html>
</body>

I want the redirect to happen only if the form is filled out correctly, so the
redirect should, I think, go just after the form code.
Jun 12 '06 #6

P: n/a
deko wrote:
You must ensure that there is no blank space or empty lines, outside
of the <?php ?> braces. Any such empty space will be sent and will
cause headers to already be sent.

Make sure that <?php is the very first thing on the very first line of
the file.


but what about all the html?

the page looks like this:

DOCTYPE ...
<html>
<head>
...
javascript...
</head>
<body>
<html>
all kinds of html...
<form>
<?php
...
?>
...
</form>
more html...
</html>
</body>

I want the redirect to happen only if the form is filled out correctly,
so the redirect should, I think, go just after the form code.

The form processing should be the *first* thing in the file.
Something like:

DOCTYPE ...
<?php
if( isset($_POST['foo'] ) {
// do form processing

if( $isValid ) {
header();
exit;
}
}
?>
<form action="..." method="POST">
....
<input type="text" name="foo" size="10" maxlength="255">
<input type="submit">
</form>

-david-

Jun 12 '06 #7

P: n/a
deko wrote:
You must ensure that there is no blank space or empty lines, outside
of the <?php ?> braces. Any such empty space will be sent and will
cause headers to already be sent.

Make sure that <?php is the very first thing on the very first line
of the file.
but what about all the html?

The html will be in the page that it is redirected to.

If you are redirecting there is no need to send any html for THIS page
because you are redirecting to a different page.


the page looks like this:

DOCTYPE ...
<html>
<head>
...
javascript...
</head>
<body>
<html>
all kinds of html...
<form>
<?php
...

...
</form>
more html...
</html>
</body>

I want the redirect to happen only if the form is filled out
correctly, so the redirect should, I think, go just after the form
code.


Jun 12 '06 #8

P: n/a
> The form processing should be the *first* thing in the file.
Something like:

DOCTYPE ...
<?php
if( isset($_POST['foo'] ) {
// do form processing

if( $isValid ) {
header();
exit;
}
}
?>
<form action="..." method="POST">
...
<input type="text" name="foo" size="10" maxlength="255">
<input type="submit">
</form>


Thanks, that helped. I am getting the correct redirect behavior now.

But I still have a couple of questions...

Here's how it looks now:

------------------------
<?php
$message = $_POST['message'];
$email = $_POST['email_address'];
if (!empty($message) && !empty($email))
{
header("Location:http://bubba/send-confirm.php");
exit;
}
?>
DOCTYPE
<html>
....
....
<form action=<?php include "myscript.php" ?> method="POST">
<input name="email_address" type="text" size="10" maxlength="255">
<textarea name'"message" rows="12" cols="22"> </textarea>
<input type="submit" value="Send">
</form>
....
....
</html>
--------------------------

Does my form look correct?

What form attributes are required (which are optional) - name, type, method... ?

Am I properly referencing "myscript.php"? Do I need the include statement?

Thanks again for you help!
Jun 12 '06 #9

P: n/a
deko wrote:
The form processing should be the *first* thing in the file.
Something like:

DOCTYPE ...
<?php
if( isset($_POST['foo'] ) {
// do form processing

if( $isValid ) {
header();
exit;
}
}
?>
<form action="..." method="POST">
...
<input type="text" name="foo" size="10" maxlength="255">
<input type="submit">
</form>


Thanks, that helped. I am getting the correct redirect behavior now.

But I still have a couple of questions...

Here's how it looks now:

------------------------
<?php
$message = $_POST['message'];
$email = $_POST['email_address'];
if (!empty($message) && !empty($email))
{
header("Location:http://bubba/send-confirm.php");
exit;
}
?>
DOCTYPE
<html>
...
...
<form action=<?php include "myscript.php" ?> method="POST">
<input name="email_address" type="text" size="10" maxlength="255">
<textarea name'"message" rows="12" cols="22"> </textarea>
<input type="submit" value="Send">
</form>
...
...
</html>
--------------------------

Does my form look correct?

What form attributes are required (which are optional) - name, type,
method... ?

Am I properly referencing "myscript.php"? Do I need the include statement?

Thanks again for you help!

Assuming that all this is saved in a file named 'myscript.php', then
your form should look like this:

<form action="myscript.php" method="POST">
<input type="text" name="email_address" value="<?php echo
$_POST['email_address'];?>" size="10" maxlength="255">
<textarea name="message" rows="12" cols="22">
<?php echo $_POST['message'];?>
</textarea>
<input type="submit" value="Send">
</form>

The <?php echo bits cause the form to redisplay what was entered since
the only times the user will see the form are a) if this is the first
time or b) if there was some error in processing the values supplied to
the form.

The include statement is used to read in code from another file and
merge it into the current image and then interpret it. In a form action,
you are just specifying the name of the php file that the browser should
call when the submit action of the form is triggered.

The typical minimum set of attributes on <input> tags are type and name.

-david-

Jun 12 '06 #10

P: n/a
making progress...
The <?php echo bits cause the form to redisplay what was entered since the
only times the user will see the form are a) if this is the first time or b)
if there was some error in processing the values supplied to the form.


I was just trying to put the form processing code into a separate file (not echo
it). That's more of a managability thing - for troubleshooting, I'll put the
code in-line.

I'm still having trouble getting the form action working:

<form action="<?php
//write to file for testing - production code will send email
$message_file = "/home/username/public_html/cgi-bin/messages.txt";
$message = $_POST['message'];
$fp_msg = fopen($message_file,"a");
fwrite($fp_msg, $message."\n");
fclose($fp_msg);
?>" method="post" name="feedback" >
<textarea name="message" rows="12" cols="22" maxlength="4096"></textarea></p>
<input type="submit" value="Send">
</form>

The problem is nothing gets written to messages.txt!

The redirect is working, now that I have this code at the very top of the page:

<?php
$message = $_POST['message'];
$email = $_POST['email_address'];
if (!empty($message) && !empty($email))
{
header("Location:http://www.mysite.com/send-confirm.php");
exit;
}
?>

But does this prevent the form's in-line code in the from running?

Should I put the form action code in just above the redirect?

A couple of other questions:

Is "4096" an acceptable value for maxlength? That's about a full page of text
(counting spaces).

Should I use the $_REQUEST['message'] variable? I've seen this in a couple of
examples - what is it for?

Thanks!

Jun 13 '06 #11

P: n/a
deko wrote:
making progress...
The <?php echo bits cause the form to redisplay what was entered since
the only times the user will see the form are a) if this is the first
time or b) if there was some error in processing the values supplied
to the form.
I was just trying to put the form processing code into a separate file
(not echo it). That's more of a managability thing - for
troubleshooting, I'll put the code in-line.

I'm still having trouble getting the form action working:

<form action="<?php
//write to file for testing - production code will send email
$message_file = "/home/username/public_html/cgi-bin/messages.txt";
$message = $_POST['message'];
$fp_msg = fopen($message_file,"a");
fwrite($fp_msg, $message."\n");
fclose($fp_msg);
?>" method="post" name="feedback" >
<textarea name="message" rows="12" cols="22"
maxlength="4096"></textarea></p>
<input type="submit" value="Send">
</form>


I think you are confused about what the 'action' attribute is about. The
'action' is used by the browser to process the 'submit'. As such, it is
executed on the browser system not the server system.
The problem is nothing gets written to messages.txt!

The redirect is working, now that I have this code at the very top of
the page:

<?php
$message = $_POST['message'];
$email = $_POST['email_address'];
if (!empty($message) && !empty($email))
{
header("Location:http://www.mysite.com/send-confirm.php");
exit;
}
?>

But does this prevent the form's in-line code in the from running?

Should I put the form action code in just above the redirect? (see below: There is a required separation of the processing of the data
and the capture of the data you need to understand.)
A couple of other questions:

Is "4096" an acceptable value for maxlength? That's about a full page
of text (counting spaces). Yes. maxlength is more typically used in conjunction with databases
where the text fields have pre-defined maximum sizes.
Should I use the $_REQUEST['message'] variable? I've seen this in a
couple of examples - what is it for? $_REQUEST is used if you don't know if the form will be sending data via
the POST or GET methods. REQUEST shows both. Personally, I tend to stick
with POST unless I am moving between web portals since POST obscures
the data being transmitted between the browser and the server a bit more.
Thanks!


Here's the deal.
1. break your code into two pieces. I'll call them email.form.html and
email.ctrl.php (ctrl for controller).
2. set up your form as follows:
DOCTYPE ...
<html>
<head>
</head>
<body>
<form action="email.ctrl.php" method="POST">
<input type="text" ... (as per previous email)
</form>
</body>
</html>

Notice that this file is pure HTML.

3. Now set up your controller as:

<?php
if( isset($_POST['message']) {
//write to file for testing - production code will send email
$message_file = "/home/username/public_html/cgi-bin/messages.txt";
$message = $_POST['message'];
$fp_msg = fopen($message_file,"a");
fwrite($fp_msg, $message."\n");
fclose($fp_msg);
}
header("location: email.form.html");
?>

4. Now execute your form - the message should be stored on your browser.

5. In depth:
a) the HTML code in email.form.html paints the form
b) the user fills out the form and clicks the submit button
c) the browser puts the form data into a POST action and sends it the
email.ctrl.php program.
d) the email.ctrl.php sees if there is any POST data and, if so,
saves it to the file
e) it then redirects the browser by telling the browser to load the
email.form.html page again.
f) we are back at a)

This is a rudimentary MVC model where the form is the View and the ctrl
is the Controller.

Now, some people combine the ctrl and the form into one file. In this
case, you would place the ctrl code before the form code in the file. I
tend not to do this because I like to separate the business/validation
logic from the presentation code, but others argue that its nice to have
all the code in one place. Either will work.

Once this is working, you can flesh it out with integrity checks and
other input types (email address, subject, CC, BCC, etc.)

-david-

Jun 13 '06 #12

P: n/a
This seems to be working...

This goes at the top of the page, before any other header info is sent:

$message = $_POST['message'];
$email = $_POST['email_address'];
if (!empty($message) && !empty($email))
{
header("Location:http://www.mysite.com/send-confirm.php");
mail("me @ mysite.com", "Feedback Form", $message, "From: $email");
exit;
}

This form can go anywhere in the page:

<form action="" method="post" name="feedback">
<input name="email_address" type="text" size="30" maxlength="255" /><br />
<textarea name="message" rows="12" cols="22" maxlength="4096"></textarea>
<input type="submit" value="Send">
</form>

Note that the form action is: ""

I have not tested this fully yet - it works writing the message to a file (as in
my previous example), but I don't know if it will send the message as an email.

The goal of this exersize was simply to redirect the user to a confirmation page
after successfully sending a message via an in-page feedback form on my web
site.
Jun 13 '06 #13

P: 3
set the action of the form to the page u want to redirect it to.
example:
<form name="form1" action="yourpage.php" ... >

If you are not submitting the page then use the function header();
the warning "Warning: Cannot add header information - headers already sent by (output started at ..." is b'coz output buffering is not initialised.
use @ob_start() as the first line of you code.

hope this helps ..

After the "send" button is clicked, I want the user to be redirected to a
different page that says "Your message has been sent."

How do I do this?

I've tried using:

header("Location:http://www.mysite.com/send-confirm.php");

But here's the error I get:

Warning: Cannot add header information - headers already sent by (output started
at ...

Here's the code:

<form action= "
<?php
$message = trim($_REQUEST['message']);
$email_address = trim($_REQUEST['email_address']);
if (empty($message) || empty($email_address))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
?>"
<input name="email_address" type="text" size="30"/><br />
<textarea name="message" rows="12" cols="23"></textarea><br />
<input type="submit" value="Send">
</form>
<?php
if ($valid_message)
{
echo "valid message";
header("Location:http://www.mysite.com/send-confirm.php");
exit();
}
else
{
echo "invalid message";
}
?>

How do I redirect after the user clicks the send button?

Thanks in advance.[/quote]
Jun 13 '06 #14

This discussion thread is closed

Replies have been disabled for this discussion.