Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2013, 10:47 AM   PM User | #1
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
Question redirect not working?!

Hi,

I am having a problem trying to redirect my website to a new page when I have filled out a form, it submits and just sits on a blank page...

here is the code I used, but it doesn't work
PHP Code:
<?php header('Location: http://www.mywebsite.com/thankyou');?>


PHP Code:
<? // Save Votes
error_reporting(E_ALL);
session_start();
include(
"connect.php");

$poll_id $_POST['poll_id'];
$vote_id $_POST['opt'];
$firstname $_POST['firstname']; 
$emailaddress $_POST['emailaddress']; 
$renewaldate $_POST['renewaldate']; 
$ses session_id();


$q mysql_query("select * from polls order by id desc limit 1");
$r mysql_fetch_array($q);
$pid $r['id'];

$opt explode("|",$r['poll_options']);
$op count($opt)-2;
$options "";

for(
$z=0;$z<=$op;$z++){
if (
$vote_id == $z){
$selectedvote $opt[$z];
}
}



$mail_To="john@john.com";
$headers "";
$headers .= "From: john@john.com\n";
$headers .= "Reply-To: john@john.co.uk\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
$headers .= "X-Mailer: php";
$mail_Subject " New vote submitted from Boat-Zone";
$mail_Body "Name: ".$firstname."<br />Email: ".$emailaddress."<br />Voted for: ".$selectedvote;

 if (
mail($mail_To,$mail_Subject,$mail_Body,$headers)) {
   
$q mysql_query("insert into poll_votes (poll_id,vote_id,firstname,emailaddress,renewaldate,ip)values('$poll_id','$vote_id','$firstname','$emailaddress','$renewaldate','$ses')");
   
header('Location: http://www.mywebsite.com/thankyou');
  } else {
   echo(
"<p>Message delivery failed...</p>");
  }

?>
jarv is offline   Reply With Quote
Old 01-30-2013, 11:16 AM   PM User | #2
ckirkster
New to the CF scene

 
Join Date: Jan 2013
Location: UK
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
ckirkster is an unknown quantity at this point
First check and ensure that your php file is UTF-8 so that there are no hidden characters at the start of the script.
If that's not the problem then comment out
PHP Code:
error_reporting(E_ALL); 
and run it again.

I'm assuming that it's getting into the "if" statement and running the insert?
ckirkster is offline   Reply With Quote
Old 01-30-2013, 12:04 PM   PM User | #3
jarv
Banned

 
Join Date: Mar 2007
Posts: 1,523
Thanks: 116
Thanked 0 Times in 0 Posts
jarv can only hope to improve
yes it's getting into the IF statement and inserting into the database, just won't redirect, I have also removed
Code:
error_reporting(E_ALL);

still nothing
jarv is offline   Reply With Quote
Old 01-30-2013, 01:56 PM   PM User | #4
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
I usually put the error reporting and the redirect right after the form like this:
PHP Code:
</form>
<?php
if ($_POST && $errors) {
    echo 
'<ul>';
    foreach (
$errors as $error) {
        echo 
"<li>$error</li>";
    }
    echo 
'</ul>';
} elseif (
$_POST && !$errors) {
    echo 
"<script>window.location = 'thankYou.php'</script>";
}

?>
Above the HTML I setup things like e-mail validation and checking for duplicates in the database. I use code like this before the mailto even starts. This is from a contact form. All my contact forms use duplicate e-mail addresses for verification. The forms also always insert submission info into a table.
PHP Code:
$editFormAction $_SERVER['PHP_SELF'];
if (isset(
$_SERVER['QUERY_STRING'])) {
  
$editFormAction .= "?" htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset(
$_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    
$errors = array();
    
// remove leading and trailing spaces from input
    
$_POST array_map('trim'$_POST);
    
    
// validate the email *** REQUIRES PHP 5.2 ***
    
if (!filter_input(INPUT_POST'eMail'FILTER_VALIDATE_EMAIL)) {
        
$errors[] = 'Please enter a valid email address';
    }
    if (
$_POST['eMail'] != $_POST['eMail2']) {
        
$errors[] = 'E-mail does not match';
    }
    
    
// go ahead if no errors are detected
    
if (!$errors) {
        
// generate a unique token
        
$token md5(uniqid(mt_rand(), true));
        
$timeNow date(time());
        
  
$insertSQL sprintf("INSERT INTO contact (firstName, lastName, eMail, timeNow, token) VALUES (%s, %s, %s, %s, %s)",
                       
GetSQLValueString($_POST['firstName'], "text"),
                       
GetSQLValueString($_POST['lastName'], "text"),
                       
GetSQLValueString($_POST['eMail'], "text"),
                       
GetSQLValueString($timeNow"text"),
                       
GetSQLValueString($token"text"));

  
mysql_select_db($database_civTekDB$civTekDB);
  
$Result1 mysql_query($insertSQL$civTekDB);
  
        
$firstName $_REQUEST['firstName'] ;
        
$lastName $_REQUEST['lastName'] ;
        
$eMail $_REQUEST['eMail'] ;

 
  
  
// generate an error message if the eMail is already in use
  
if (!$Result1 && mysql_errno() == 1062) {
      
$errors[] = $_POST['eMail'] . ' is already in use. Please use a different E-mail.';
    } elseif (
mysql_error()) {
      
$errors[] = 'Sorry, there was a problem with the database. Please try later.';
    } else 
// ==== if all OK send an email to the user
// the rest of your send mail code follows . . . 
This will give you clean error reporting right under the form and the redirect should always work when the submission is a success.

The only thing that I can think of that is breaking your code is that there is nothing to tell the redirect to work after the e-mail is sent. I don't see the problem but maybe this solution will work better for you.

Last edited by rgEffects; 01-30-2013 at 01:59 PM..
rgEffects is offline   Reply With Quote
Reply

Bookmarks

Tags
php, redirect

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:35 AM.


Advertisement
Log in to turn off these ads.