SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    After "Submit", redirect back to previous page

    Hi everyone,

    On my jokes site, my users can post comments.
    Once they click on "SUBMIT button", another page called putjokesinDB . php gets called.

    below are the codes.



    <?php
    $con = mysql_connect("localhost","myusername","mypassword");
    mysql_select_db("jokesdb", $con);

    $comment=mysql_real_escape_string($_POST['comment']);
    $sql="INSERT INTO comments (comment) VALUES ('$comment')";

    echo "Comment has been made";
    mysql_close($con);
    ?>


    <html>
    <head>

    <script language="JavaScript">

    var time = null
    function move() {

    window.location = ??? WHAT DO I PUT HERE ???
    }
    //-->
    </script>
    </head>
    <body onload="timer=setTimeout('move()',1000)">
    <h2>Redirecting back to main</h2>

    </body>
    </html>

    I tried to google out for answers, but can't find any that works well.
    All advice, tips and help is greatly appreciated! thanks all.

  2. #2
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in your previous page save the $_SERVER['QUERY_STRING']; into a session var
    and in the next page:
    PHP Code:
    <?php
    session_start
    ();

    $ref $_SESSION['ref'];


    $con mysql_connect("localhost""myusername""mypassword");
    mysql_select_db("jokesdb"$con);

    $comment mysql_real_escape_string($_POST['comment']);
    $sql "INSERT INTO comments (comment) VALUES ('$comment')";
    echo 
    "Comment has been made";
    mysql_close($con);

    ?>
    <html>
    <head>
    <script language="JavaScript">
    var time = null
    function move() {

    window.location = "<?php echo $ref ?>";

    }
    //-->
    </script>
    </head>
    <body onload="timer=setTimeout('move()',1000)">
    <h2>Redirecting back to main</h2>

    </body>
    </html>
    my mobile portal
    ghiris.ro

  3. #3
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    in your previous page save the $_SERVER['QUERY_STRING']; into a session var
    sorry, I am such a newbie lol...
    any idea how to do like what you said as above ?

    by the way, this is my codes from the previous page.

    PHP Code:

    //A TEXT BOX FOR USER TO LEAVE COMMENTS
    echo "Post Comments";
    echo
    "<form method='POST' action='putjokesinDB.php' NAME='comment_form' onSubmit='return commentempty();'>";

    echo 
    "<p><textarea rows='6' name='comment' cols='45' maxlength='1000'></textarea><br>";

    echo 
    "<p><input type='submit' value='Submit' name='B1'></p>";
     echo 
    "</form>"

  4. #4
    play of mind Ernie1's Avatar
    Join Date
    Sep 2005
    Posts
    1,252
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it should be $_SERVER['SCRIPT_NAME'];
    PHP Code:
    <?php
    session_start
    ();

    $_SESSION['ref'] = $_SERVER['SCRIPT_NAME'];

    //A TEXT BOX FOR USER TO LEAVE COMMENTS

    echo "Post Comments";
    echo
    "<form method='POST' action='putjokesinDB.php' NAME='comment_form' onSubmit='return commentempty();'>";

    echo 
    "<p><textarea rows='6' name='comment' cols='45' maxlength='1000'></textarea><br>";

    echo 
    "<p><input type='submit' value='Submit' name='B1'></p>";

    echo 
    "</form>";
    my mobile portal
    ghiris.ro

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    window.location = "http://example.com/foo/bar.html";
    Make sure to provide a regular html link for them to click as well, because not everyone enables javascript(this is why your usually told "click if you are not redirected within 5 seconds").

  6. #6
    SitePoint Enthusiast aaron888's Avatar
    Join Date
    Apr 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ernie1 View Post
    it should be $_SERVER['SCRIPT_NAME'];
    PHP Code:
    <?php
    session_start
    ();

    $_SESSION['ref'] = $_SERVER['SCRIPT_NAME'];

    //A TEXT BOX FOR USER TO LEAVE COMMENTS

    echo "Post Comments";
    echo
    "<form method='POST' action='putjokesinDB.php' NAME='comment_form' onSubmit='return commentempty();'>";

    echo 
    "<p><textarea rows='6' name='comment' cols='45' maxlength='1000'></textarea><br>";

    echo 
    "<p><input type='submit' value='Submit' name='B1'></p>";

    echo 
    "</form>";

    a big thanks to Ernie1.

    it works!!!
    but I did a little more research, and found out REQUEST_URI is the most suitable one for dynamic sites.


    ----------------------------------------------------------------


    hello there my lovely crmalibu, good to see you.

    errr.. yeah..good point. but the problem is .. my jokes site is dynamic..
    its has like mydomain.com/comments.php?id=1218


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
  •