473,854 Members | 1,721 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
13 70016
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($messag e) && !empty($email))
{
header("Locatio n: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($messag e) && !empty($email))
{
header("Locatio n: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.c trl.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("locatio n: 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($messag e) && !empty($email))
{
header("Locatio n: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_add ress" 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 New Member
set the action of the form to the page u want to redirect it to.
example:
<form name="form1" action="yourpag e.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("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.[/quote]
Jun 13 '06 #14

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
9752
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
11031
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
10685
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...
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...
1
7918
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5750
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4162
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.