Help reqd with form design

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Help reqd with form design

Post by Live24x7 »

Please help me with this simple form design -

For simplicity, lets say my form has 2 fields - name and email.
On submit, i want to do 3 things:

1) Send email
2) Insert into MYsql Db
3)redirect to a thankyoupage
(say thankyou.php)
I have no issues with 1 and 2 above, but when i try to redirect using header

Code: Select all

('Location: thankyou.php');
i get the - header already sent error

My form flow is somewhat like this

Code: Select all

<?php
 if (isset($_POST['submit'])) 
				{ 	
                              include (connect.php) // DB connect
				$rname= $_POST['rname']; 
                                  $remail= $_POST['remail']; 
				// some php form validation 
				//php to send email code
				// php to Insert into DB 
				header('Location: thankyou.php'); // - this is where i am getting stuck
					}}else {
					
					include_once ('header.php'); 

 ?>
 
			<!-- Form Code Start -->
			 <h2>Member Registration</h2> <br/>
				<form name="rform" class='form' action="'' method='POST' >
				<label>Name </label> <input type='text' name='rname' maxlength="20" /> <br/>
				<label>Email</label>  <input type='text' name='remail' maxlength="40" /><br/>
				<input class='button' type='submit' name='submit' value='SUBMIT TO JOIN'/>    
				</form>	<!-- Form Code Ends --> 		
<?php } ?>

I am now using a JS redirect to keep this form functioning but, i want to use php to redirect to thankyou page

Do i need to change the form structure ?

PLease advise. :roll:
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Help reqd with form design

Post by califdon »

This is not a form design issue, it is simply a PHP issue. As you have found, if a script has sent anything at all to the browser (even a blank line or a linefeed character) then you cannot send a redirect header after that. The solution is to structure your script so that absolutely nothing is sent to the browser until you have (if you're going to) sent the redirect header. Since you haven't shown us any other code than you have, all I can tell you is that you need to think about the structure of your entire script and modify it to meet the above criterion.
Live24x7
Forum Contributor
Posts: 194
Joined: Sat Nov 19, 2011 9:32 am

Re: Help reqd with form design

Post by Live24x7 »

thankyou califdon, will rework it out.
Post Reply