Have an account? Sign in
Login  Register  Facebook
How to remember POST value when change the URL?
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.
Started: September 21, 2011 Latest Activity: September 21, 2011 post remember url

Thanks friends, I have solved the problem. Have a nice day. :}
September 21, 2011

4 Answers
What you're trying to achieve seem a little convulted to, and I think it would be wise to rethink your solution when you have more ideas about how you could solve what you're trying to do.

The way I understand your process flow you have this: (pseudocode)

a.php posts form with field text(id=a1) to b.php
b.php processes the form and creates a link to b.php?id=<url>
when link is clicked b.php is run again, but without form this time only get.

So b.php does two things:
  • process the form and generate a table with links
  • process the links from the table


It seems to me you're mixing the two and forgets that they are separate requests.

There are several ways you can achieve what you want of course. here is a list of a couple:
  • You can do as is mentioned above: add the variables you want from your form to your url and retrieve them from _GET istead of _POST
  • Turn on sessions on your page and store the variables from _POST in the session and retrive them form there on the next request
  • create a new form with in b.php when creating the table and resubmit the form to be again.


Hope this helps.

Posted: greentree
In: September 21, 2011

Pass the value of $_POST["a1"] in another parameter in the b.php?... URL so that it can be accessible in b.php's $_GET lookup later.

Maybe it's a cut-and-paste error, but this:
href="b.php?id=".$row["url"]."" target="_self"
doesn't make any sense, you seem to be trying to use double quotes inside a double-quoted string. Also, you're forgetting to HTML-encode your output, which results in XSS security holes, and you will also need to URL-encode content you're putting into a URL parameter.
<?php
    $url= 'b.php?id='.urlencode($row['url']).'&a1='.urlencode($_POST['a1']);
?>
<a href="<?php echo htmlspecialchars($url); ?>">
    <?php echo htmlspecialchars($row['title']); ?>
</a>
then
<li>
    <?php echo htmlspecialchars($row['name']); ?>
    (<?php echo htmlspecialchars($_GET['id']); ?>,
    <?php echo htmlspecialchars($_GET['a1']); ?>)
</li>
(You can define a helper function with a shorter name like h() to avoid having to type out echo htmlspecialchars() so much.)

Posted: MacOS
In: September 21, 2011

If you want pass data you can put it in link as query string and get it again with GET or put it in SESSION or COOKIE

Posted: greentree
In: September 21, 2011

Thanks for anwser. How to put a value in SESSION or COOKIE?
September 21, 2011

Just add this value to to the query string of the link.
and change POST method to GET

Posted: Go
In: September 21, 2011

Your Answer

xDo you want to answer this question? Please login or create an account to post your answer