tags:

views:

69

answers:

4

I have some datas that are submitted with POST method on the same page. I have some actions and layout which follow up using these datas. Among those actions, I also have a quick action which sends the parameter in query string. When I click on this link, I can fetch the action that I am trying to do, but I loose all the variables from my POST, which I need to display the same page again.

Is there a way, that I can preserve POST variables, for multiple reloading times.

+3  A: 

You can try saving that data in a session instead ?

youssef azari
NO that is the not the way I am looking for...
Starx
You're stuck because there's no way to do that.You can't preserve POST, it's something that dies when the request is served...
youssef azari
A: 

You'll need to parse the data in $_POST and recreate it in your form. You can do it with hidden fields.

Or, you can save the $_POST data in a user session and refer to it when you need it. You'll have to manage the lifecycle of the data to make sure it doesn't stay around too long.

Scott Saunders
I am sending the query string using links, so i am not using form to post those
Starx
Then you'll have to parse $_POST into parameters and add them to your link. They'll be available to your script in $_GET and $_REQUEST.
Scott Saunders
the cure is even worst than the disease. what if there are large text or some sensitive information like password?
Col. Shrapnel
@Col. Shrapnel: I believe I've answered with the only options: parse the array and repost it, or use a session. As you can see, the OP has not given us enough information to comment on the best method for his circumstances - though it looks to me like a complete redesign and rewrite is in order.
Scott Saunders
@scott, I am using the exact way you are saying... and as @Col. Shrapnel said, posting these values in url will leave the system vulnerable to injection..... That's why I was wondering if there was a way :( to do this
Starx
Injection is not the problem he was talking about. Injection is just as possible with POSTed forms. The problem with adding stuff to the URL is the max length of the URL is shorter than what you can POST and all of your data is visible to users who 'view source' (which is also a problem with rePOSTing from a form). Either way, your three options have been outlined in a number of answers: resubmit the data, use a session, or redesign your application. There is no other way.
Scott Saunders
A: 

The problem is that you are sending the data in via the URL which is stored in the $_GET variable, not in $_POST. If you want the ability to submit the data in either format, use $_REQUEST instead.

There is a bit of a debate on whether it is a good idea to use $_REQUEST, but if you are doing a simple site, there is little wrong with it.

If you would rather not use $_REQUEST, then you can use the following code on each variable you are expecting:

if (!empty($_GET['foo'])) {
    $foo = $_GET['foo'];
} elseif (!empty($_POST['foo'])) {
    $foo = $_POST['foo'];
} else {
    die("Foo not submitted");
}
Joseph
A: 

I would suggest not using $_POST but using $_REQUEST. This means you can format your links thus, which will preserve all of the data in $_REQUEST:

function addParams($link){
  $result = $link;
  $first = true;
  foreach($_REQUEST as $key=>$value){
    $link .= ($first) ? '?' : '&';
    $first = false;
    $link .= "$key=$value";
    }
  return $link;
  }

...

echo "<a href='".addParams('nextpage.php')."'>Next Page</a>";

However I don't condone this method, you should use $_SESSION!

fredley
I would suggest against that. Separate arrays were added on purpose. Do not mix inputs. If you gonna use a link - make your form using GET method already, no post needed. And if you use POST - there should be a redirect, not a page with links. And learn to format your query string properly
Col. Shrapnel
I did add a disclaimer, this is a horrible thing to do, but it would work. Re: formatting, you would just need to use urlencode. Starx: What are your reasons for not using $_SESSION? You do realise that you can store arrays in $_SESSION, so you can store data in $_SESSION['post']?
fredley