Results 1 to 5 of 5

Thread: redirecting thingy in a script

  1. #1
    Join Date
    Dec 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default redirecting thingy in a script

    Hey, I'm using the script found here
    http://www.zimmertech.com/tutorials/...t-tutorial.php

    down in the third part
    Code:
    <?php
    //Please set the following variables for your mysql database:
    $db_hostname = "localhost";  //usually "localhost be default"
    $db_username = "";  //your user name
    $db_pass = "";  //the password for your user
    $db_name = "";  //the name of the database
    
    
    /*MYSQL DATABASE CONNECTION/ TRACKING FUNCTIONS
    --------------------------------------*/
    // connect to database
    $dbh = mysql_connect ($db_hostname, $db_username, $db_pass) or die ('I cannot connect to the database because: ' . mysql_error());
    mysql_select_db ($db_name);
    
    
    $tuturl = $_POST["tuturl"];
    $tutid2 = $_POST["tutid2"];
    $name = $_POST["name"];
    $url = $_POST["url"];
    $email = $_POST["email"];
    $message = $_POST["message"];
    
    $sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");
    if($sendcomment){
    //header("Location: $tuturl");
    echo "<h1>Submission Successful</h1>";
    echo "Your comment has been submitted.  You will now be redirected back to the last page you visited.  Thanks!";
    echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
    } else {
    echo "There was an error with the submission. ";
    }
    
    ?>
    the redirect is kinda weird, it isn't working
    I know absolutely almost nothing about php

    how do I change the redirect to be the page you placed the comment?

    Thanks,
    Briggs

  2. #2
    Join Date
    Feb 2008
    Location
    Coventry
    Posts
    103
    Thanks
    5
    Thanked 8 Times in 8 Posts

    Default

    Hmmm it works for me. Make sure that the URL that is being passed through has 'http://' on the front of it and if it doesnt you'll need to append to it.

    Also make sure that the variable is actually set and passed through, which brings me to if($sendcomment) {

    Do you want to see if it has worked? that the insert was successful?
    if u want to see if its been completed then do something like this

    Code:
    if($sendcomment == "1") { 
    echo "<h1>Submission Successful</h1>";
    echo "Your comment has been submitted.  You will now be redirected back to the last page you visited.  Thanks!";
    echo "<meta http-equiv='refresh' content='2;URL=$tuturl'>";
    } else {
    echo "There was an error with the submission. ";
    }
    I think it is 1 that it gets set to. Only one way to find out and thats to take out the redirect for a minute and just echo out the variable to see what it prints and then you know
    The important thing is not to stop questioning. Curiosity has its own reason for existing.

  3. #3
    Join Date
    Dec 2007
    Posts
    6
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    Browsers expect the <META> element to be in the head of the page. I mean between the <head> and </head> tags. If you will place it there, it will work in all browsers.

    You can do that if you put all the code at the beginning of the page, and leave only echo statements in the body:

    PHP Code:
    <HTML>
    <HEAD>
    <TITLE>My great PHP page :)</TITLE>

    <?php
    //Please set the following variables for your mysql database:
    $db_hostname "localhost";  //usually "localhost be default"

    //..........ALL THE STUFF FROM ABOVE....................

    $sendcomment mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");

    if(
    $sendcomment) echo "<META http-equiv='refresh' content='2;URL=$tuturl'>";
    ?>

    </HEAD>
    <BODY>

    <?php
    if($sendcomment){
        echo 
    "<h1>Submission Successful</h1>";
        echo 
    "<p>Your comment has been submitted.  You will now be redirected back to the last page you visited.  Thanks!</p>";
    } else {
        echo 
    "<p>There was an error with the submission.</p>";
    }
    ?>

    </BODY>
    </HTML>

  4. #4
    Join Date
    Dec 2007
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I probably should mention that I'm using a php template

    edit - Ok, I think I figured out a solution, I just added a "Go back" link in stead of it automatically taking you back
    Last edited by Briggs; 03-17-2008 at 04:42 PM.

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    try

    Code:
    $message = $_POST["message"];
    
    $sendcomment = mysql_query("INSERT INTO comments SET tutorialid='$tutid2', name='$name', url='$url', email='$email', comment='$message', date=now()");
    
    if($sendcomment){
         // N is the number of seconds to redirection
         header("Location: $tuturl",N);
         echo "<h1>Submission Successful</h1>";
         echo "<p>Your comment has been submitted. Thanks!</p>";
         echo '<p>You will now be redirected in N seconds. <a href="'.$tuturl.'">Click Here if you are not redirected</a>';
         exit();
    } 
    else {
         echo "<p>There was an error with the submission.</p>";
    }
    
    ?>
    that will keep with your automated redirect, but it will give the user a chance to redirect themselves manually if there is an error

Bookmarks

Posting Permissions

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