473,416 Members | 1,660 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,416 software developers and data experts.

How to redirect after form submit?

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
13 69965
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
> 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
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
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
> 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
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
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
> 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
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
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
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
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
gauri
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: shake | last post by:
I have to develop an application sounds like this:- User is allow to access a form (is actually a test/quiz) within a specific time frame, let say 45 minutes. After this 45 minutes, if the user...
2
by: Matt | last post by:
Can form.submit() submit another form? In the following example, in page1.asp, it is fine to submit the current form: form1.submit(), and and I could see the value "Joe" in page2.asp. However, if I...
2
by: Hongyu | last post by:
I am trying to implement a simple JavaScript of redirecting my window to a new URL upon clicking a submit button. This is an easy task except when I have to put an input type='submit' in front of...
5
by: Scott | last post by:
How can I tell a form to submit itself in the code-behind in vs.net? In other words, in javascript I can do blah.submit() - how do I do this in vs.net code-behind?
12
by: TheOne | last post by:
In Asp.net web form under form tag there is action field that I am point to some other page, and not to same web form. When I run this page it is always pointing to itself. How do I get around...
4
by: jwlum | last post by:
I have the following problem under Internet Explorer only: 1. User fills out form data (myform.php) and clicks a button that fires myFunction() 2. myFunction() spawns a "hello, world" popup page...
3
by: prodizy | last post by:
Hi, In Firefox, what's the best way of tracking the form submit? The following are two ways I tried, but they won't work if the form is submitted through JavaScript. Method 1: using the...
1
by: gbezas | last post by:
Hi All, I have added an event handler to redirect form.submit() to a newSubmit() method that I have defined (which does some additional processing before submitting the form). Additionally I...
14
by: white lightning | last post by:
How to have <select onchange="this.form.submit()"and also a Submit button on one form? I have something like this: <form action="<?php $_SERVER; ?>" method="post"...
13
Frinavale
by: Frinavale | last post by:
I've been trying all morning to cancel a form submit to the server. I have a JavaScript Object that determines whether or not the page should be submitted to the server depending on whether the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.