Current location: Hot Scripts Forums » Programming Languages » PHP » redirect on form submit


redirect on form submit

Reply
  #1 (permalink)  
Old 11-23-03, 04:03 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
redirect on form submit

Lol ok guys this is my last question because i've just about finished my entire script. All i was after was, when someone enters a new messege and presses submit, i want it to automatically go to the index page (where the originally are) instead of going to an ugly white page with a link back to the index.

Im using the form with the action in a seperate page, so heres the code for that page:

Code:
<HTML>
<BODY>
<?php
include("config.php");
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("D jS M y");
$sql = "INSERT INTO eight (id, subject, name, email, site, aim, yim, msn, msg, date, ip) VALUES 
('', '$_POST[subject]', '$_POST[name]', '$_POST[email]', '$_POST[site]', '$_POST[aim]', '$_POST[yim]', '$_POST[msn]', '$_POST[msg]', '$date', '$ip')";
$result = mysql_query($sql);

?>
</BODY>
</HTML>
So if u guys can tell me what i have to do to get that redirect it'll be great, i've played around with a few different codes and most of them give me errors about the headers or something. Thanks in Advance!
Reply With Quote
  #2 (permalink)  
Old 11-23-03, 05:08 AM
Hackerdragons Hackerdragons is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Turkey
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
I Don't Really Get What You Mean But I Suppouse You Might Make THis:
<a href="filename.php?from=previous.php">On</a>
On Click İt'd Call Filename.php but add a value
$from ="previous.php";
then you can call previous file($from) by code
header( Location:"$_SERVER['DOCUMENT_ROOT']/$from" );
That's All. Hope I Help'd



I EDIT MY MESSAGE:
since it's on foprm action you must write
<form action="filename.php?from=previous.php" method="post">
or
<form action="filename.php?from=$SCRIPT_NAME" method="post">

Last edited by Hackerdragons; 11-23-03 at 05:11 AM.
Reply With Quote
  #3 (permalink)  
Old 11-23-03, 10:16 AM
mdhall's Avatar
mdhall mdhall is offline
Aspiring Coder
 
Join Date: Oct 2003
Posts: 510
Thanks: 1
Thanked 1 Time in 1 Post
Try this

Code:
<HTML>
<BODY>
<?php
include("config.php");
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("D jS M y");
$sql = "INSERT INTO eight (id, subject, name, email, site, aim, yim, msn, msg, date, ip) VALUES 
('', '$_POST[subject]', '$_POST[name]', '$_POST[email]', '$_POST[site]', '$_POST[aim]', '$_POST[yim]', '$_POST[msn]', '$_POST[msg]', '$date', '$ip')";
$result = mysql_query($sql);
 mysql_close();
 echo header ("Location: index.php"); 
?>
</BODY>
</HTML>
Reply With Quote
  #4 (permalink)  
Old 11-23-03, 10:43 AM
Darkness22k Darkness22k is offline
Dark Fire
 
Join Date: Jun 2003
Location: Seattle, Washington
Posts: 48
Thanks: 0
Thanked 0 Times in 0 Posts
Or this...but mdhall's is better.
Code:
<HTML>
<BODY>
<?php
include("config.php");
$ip = $_SERVER['REMOTE_ADDR'];
$date = date("D jS M y");
$sql = "INSERT INTO eight (id, subject, name, email, site, aim, yim, msn, msg, date, ip) VALUES 
('', '$_POST[subject]', '$_POST[name]', '$_POST[email]', '$_POST[site]', '$_POST[aim]', '$_POST[yim]', '$_POST[msn]', '$_POST[msg]', '$date', '$ip')";
$result = mysql_query($sql);
 mysql_close();
 include ("index.php");
?>
</BODY>
</HTML>
__________________
*~Darkness22k~* {Mark Tesn}
PHP/MySQL Programmer
Need help? Email or IM me.
Reply With Quote
  #5 (permalink)  
Old 11-24-03, 12:58 AM
simone's Avatar
simone simone is offline
Newbie Coder
 
Join Date: Nov 2003
Posts: 67
Thanks: 0
Thanked 0 Times in 0 Posts
Darkness22k to my rescue again! ur the best

mdhall, your one gave me the same error i was getting with every other code i tried.
Reply With Quote
  #6 (permalink)  
Old 11-24-03, 04:54 AM
fyrestrtr fyrestrtr is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
You should always check to make sure your query executed successfully before redirecting. Never trust the database to work

http://www.php.net/mysql-error
Reply With Quote
  #7 (permalink)  
Old 11-24-03, 01:20 PM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
A couple changes to Darkness'.....
PHP Code:

<HTML>

<BODY>
<?php
include("config.php");
$ip $_SERVER['REMOTE_ADDR'];
$date date("D jS M y");
$sql "INSERT INTO eight (id, subject, name, email, site, aim, yim, msn, msg, date, ip) VALUES 
('', '
$_POST[subject]', '$_POST[name]', '$_POST[email]', '$_POST[site]', '$_POST[aim]', '$_POST[yim]', '$_POST[msn]', '$_POST[msg]', '$date', '$ip')";
$result mysql_query($sql);
//ERROR CHECK
if (!mysql_errno() == '0') {
echo 
mysql_errno() . "<br>" mysql_error();
}
else {
include (
"index.php");
}
//END ERROR CHECK
mysql_close();
}
?>
</BODY>
</HTML>
Its always a good idea to have an error check. Like was said, don't just expect SQL to work.
Reply With Quote
  #8 (permalink)  
Old 11-24-03, 03:22 PM
fyrestrtr fyrestrtr is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 191
Thanks: 0
Thanked 0 Times in 0 Posts
Actually, this would be a better way to check for errors :

PHP Code:



if (!($result mysql_query($sql)))
{
    die(
mysql_errno()." : ".mysql_error());
} else { 
header("Location: http://www.domain.com/index.php"); exit(); } 
Reply With Quote
  #9 (permalink)  
Old 11-24-03, 04:11 PM
eddyvlad eddyvlad is offline
Wannabe Coder
 
Join Date: Sep 2003
Location: In The Bloody Pits Of Hell
Posts: 160
Thanks: 2
Thanked 0 Times in 0 Posts
As far as I know, the header function need to be executed before the </head> tag. Coz I've tried after the </head> tag and it will generate an error.
__________________
Mr. Brown Eyes
Reply With Quote
  #10 (permalink)  
Old 11-24-03, 06:10 PM
Addict's Avatar
Addict Addict is offline
Newbie Coder
 
Join Date: Nov 2003
Location: Ohio
Posts: 66
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by fyrestrtr
Actually, this would be a better way to check for errors :

PHP Code:



if (!($result mysql_query($sql)))
{
    die(
mysql_errno()." : ".mysql_error());
} else { 
header("Location: http://www.domain.com/index.php"); exit(); } 
You can't do the header function because there is already output that has been sent to the browser. So you have to include. Of course there's no reason simaone couldn't alter the code to only send the data after it was successful or unseuccessful.

And the code above will produce the same results as yours. There's not one way better than the other on that.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Why I always get \' and \" after I submit the form ! kevin PHP 4 11-24-03 04:57 AM
question on form and submit TxRanger JavaScript 2 11-03-03 11:28 AM
process form without press submit button azwani JavaScript 1 11-03-03 01:10 AM
SQL database registration form help vinhkhuong PHP 3 10-10-03 03:49 AM
Need to submit form to database and text file - Any ideas how? dpreiss ASP 1 08-21-03 06:02 PM


All times are GMT -5. The time now is 05:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.