473,854 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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("Locatio n: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_ad dress))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
?>"
<input name="email_add ress" 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("Locatio n: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 70015
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("Locatio n: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_ad dress))
{
$valid_message = false;
}
else
{
mail( "my email address", "feedback form", $message, "from:
$email_address" );
$valid_message = true;
}
"

<input name="email_add ress" 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("Locatio n: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("Locatio n: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_ad dress))
{
$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_add ress" 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("Locatio n: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("Locatio n: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($messag e) && !empty($email))
{
header("Locatio n:http://bubba/send-confirm.php");
exit;
}
?>
DOCTYPE
<html>
....
....
<form action=<?php include "myscript.p hp" ?> method="POST">
<input name="email_add ress" 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.p hp"? 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($messag e) && !empty($email))
{
header("Locatio n:http://bubba/send-confirm.php");
exit;
}
?>
DOCTYPE
<html>
...
...
<form action=<?php include "myscript.p hp" ?> method="POST">
<input name="email_add ress" 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.p hp"? 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="myscrip t.php" method="POST">
<input type="text" name="email_add ress" 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

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

Similar topics

2
17861
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 has not yet click the submit button to submit the form, the system need to automatically submit it. So, can anyone teach me how to check the time out as well as how to make the form submit automatically? Thank you very much.
2
5554
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 do: form3.submit(), then it couldn't go to page2.asp. Any ideas?? Thanks!! //page1.asp <html> <head> </head> <body onload="form1.submit()"> //OK!
2
12493
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 the onClick command. It always commits the CGI action, and skip the URL redirect part. The example code is below <form action=mycgi.pl> <input type='submit' name=submit value='Submit' onClick="window.location.href='http://myserver.com'">...
5
10993
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
3453
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 this? Here is HTML code from webform.. Thanks, Sinisa
4
8169
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 via window.open 3. myFunction() submits the main page's form via document.form.submit() 5. User closes popup window and clicks browser's Back button to return to form entry page 6. All the form data that the user had filled out is now blank...
3
7429
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 window.onsubmit event handler Code: window.onsubmit=handleEvent;
1
10843
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 have defined the relavant function method in the code for details) The issue is that when Icall targetForm._submit() method from the newSubmit() function the page I get an 'Object doesn't support this property or method' error I am using IE...
14
75561
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" enctype="multipart/form-data" name="form1"> <select onchange="this.form.submit();" name="prod"> <option value="">Select product</option> <option value="12">abc</option>
13
10575
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 user clicks "yes" or "no". This Object is attached to various HTML elements on the page (buttons, selects etc) and traps any events that they raise that cause the page to submit. If the user has not responded, the Object should cancel the page...
0
9901
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9751
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11027
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10682
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10761
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10371
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9513
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7082
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.