How to remember $_POST value when change the URL?

Discussion in 'PHP' started by youlichika, Sep 24, 2010.

  1. #1
    I have got two page.

    page a.php
    <form action="b.php" method="post">
    <input id="a1" type="text" value="keyword" name="a1" />
    <input type="submit" value="post"/>
    </form>

    page b.php
    <a href="b.php?id=".$row["url"]."" target="_self" >".$row["title"]."'.$_POST["a1"].'</a>
    <li>".$row["name"]."'.$_GET["id"].'</li>

    $row["url"],$row["title"] and $row["name"] are all query from mysql.
    After b.php received the $_POST value from a.php, it combined to a new hyperlink with $row["title"]. When click this hyperlink, the new value with ".$row["name"]." in <UL> <LI>.

    Now when the url change to b.php?id=".$row["url"].", the $_POST value from a.php lost. How to deal with this problem? Thanks.
     
    youlichika, Sep 24, 2010 IP
  2. daljit

    daljit Well-Known Member

    Messages:
    312
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    101
    #2
    you may save the post data values in an $_session array or in $_cookies.
     
    daljit, Sep 24, 2010 IP
  3. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    hello, I try to use below codes in page b, but after change the URL, it still lost the POST value.

    session_start();
    if($_SERVER['REQUEST_METHOD']=="POST") {
    $_SESSION["a1"] = $_POST["a1"] ;
    }

    session_start();
    if($_SERVER['REQUEST_METHOD']=="POST") {
    $_COOKIE["a1"] = $_POST["a1"] ;
    }
     
    youlichika, Sep 25, 2010 IP
  4. systemick

    systemick Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    How are you attempting to retrieve the data you stored in the session? On the second page you would need to reference the $_SESSION array.
     
    systemick, Sep 25, 2010 IP
  5. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #5
    Hi!

    First Print Your $_POST["a1"] Value In b.php For Test And Then USE!

    <a href="b.php?id=<?php print $row["url"]; ?>" target="_self"><?php print $row["title"] . $_POST["a1"]; ?></a>

    If You Want To Print This Line In PHP Use This:

    print '<a href="b.php?id=' . $row["url"] . '" target="_self">' . $row["title"] . $_POST["a1"] . '</a>';
     
    HungryMinds, Sep 25, 2010 IP
  6. ProvideVPN

    ProvideVPN Banned

    Messages:
    114
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I thing you need this :
    put a form like this in the page b.php
    
    <form action="c.php" method="post">
    <input id="a1" type="text" value="<?php echo $_POST['a1'];?>" name="a1" />
    <input type="submit" value="post"/>
    </form>
    
    HTML:
     
    ProvideVPN, Sep 25, 2010 IP
  7. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #7
    Hello HungryMinds, I can Print and get my $_POST value in b.php witch is posted from a.php.
    not_dp_admin: why post the value to c.php? I don't quite understand.
     
    youlichika, Sep 25, 2010 IP
  8. Ralle

    Ralle Active Member

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    91
    #8
    I'd just do this:
    
    // page one
    session_start();
    if($_POST)
    {
        $_SESSION['post'] = $_POST;
    }
    
    // page two
    session_start();
    if(!$_POST)
    {
        $_POST = $_SESSION['post'];
    }
    PHP:
     
    Ralle, Sep 25, 2010 IP
  9. HungryMinds

    HungryMinds Active Member

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #9
    This Is The Sample CODE:

    Try It And Then Tell Us What U Wanna Do Next.

    CLICK HERE TO PREVIEW

    a.php
    
    <html>
    <body>
    <form action="b.php" method="post">
    <input id="id" type="text" value="id" name="id" />
    <input id="keyword" type="text" value="keyword" name="keyword" />
    <input type="submit" value="post"/>
    </form>
    </body>
    </html>
    
    Code (markup):
    b.php
    
    <?php
    
    $row["url"] = "http://www.mysite.com/";
    $row["title"] = "My Title";
    $row["name"] = "My Name";
    
    $keyword = $_POST["keyword"];
    $id = $_POST["id"];
    
    if($keyword == "" || $id == "")
    {
    	echo "Blank Fields";
    }
    else
    {	
    
    	print '<a href="b.php?id=' . $row["url"] . '" target="_self">' . $row["title"] . $_POST["keyword"] . '</a>';
    	print '<ul>';
    	print '<li>' . $row["name"] . $_POST["id"] .'</li>';
    	print '</ul>';
    }
    
    ?>
    
    Code (markup):
     
    Last edited: Sep 25, 2010
    HungryMinds, Sep 25, 2010 IP
  10. youlichika

    youlichika Greenhorn

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    Thanks friends, I have solved this problem under your help.
    Shake hands. :}
     
    youlichika, Sep 27, 2010 IP