Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts

    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>");
      }

    ?>

  • #2
    New to the CF scene
    Join Date
    Jan 2013
    Location
    UK
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #3
    Banned
    Join Date
    Mar 2007
    Posts
    1,523
    Thanks
    116
    Thanked 0 Times in 0 Posts
    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

  • #4
    Regular Coder
    Join Date
    Aug 2012
    Posts
    142
    Thanks
    39
    Thanked 3 Times in 3 Posts
    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 02:59 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •