68

Is there a way to set a $_POST['var'] without using form related field (no type='hidden') and using only PHP. Something like

$_POST['name'] = "Denniss";

Is there a way to do this?

EDIT: Someone asked me for some elaboration on this. So for example, I have page with a form on it, The form looks something like this

<form method='post' action='next.php'>
<input type='text' name='text' value='' />
<input type='submit' name='submit' value='Submit'/>
</form>

Once the submit button is clicked, I want to get redirected to next.php. Is there a way for me to set the $_POST['text'] variable to another value? How do I make this persistent so that when I click on another submit button (for example) the $_POST['text'] will be what I set on next.php without using a hidden field.

Let me know if this is still not clear and thank you for your help.

ave
  • 287
  • 13
  • 27
denniss
  • 17,229
  • 26
  • 92
  • 141
  • 3
    You mean in the server code or from the client side when a user access some page? – Felix Kling Aug 05 '10 at 18:46
  • 2
    Can you elaborate what you are trying to do? cURL might be an option, sessions maybe another option. Too vague to give an exact answer. – Jim Aug 05 '10 at 18:46
  • 1
    What? And why do you not want to use a hidden field? That's what they're for. – Mark Snidovich Aug 05 '10 at 18:52
  • It's not that I do not want to use them. I just want to know whether there is a way to do it without using hidden field. – denniss Aug 05 '10 at 18:55
  • `whether there is a way to do it` the problem is nobody know WHAT you want to do. that's what you were asked for, because your question is nonsense. – Your Common Sense Aug 05 '10 at 19:05
  • you need to give real life task that led you to that question. otherwise it will be waste of your and our time – Your Common Sense Aug 05 '10 at 19:07
  • 3
    You cannot persist data between page accesses without using a session or cookies. Every variable that you set in page a.php will be lost when page b.php is accessed. It seems you want to use a certain way to do something although this way is not suitable for the problem. – Felix Kling Aug 05 '10 at 19:08
  • @Col. Sharpnel Someone has answered my question already. @Felix Kling Thanks – denniss Aug 05 '10 at 19:11
  • You are right about me knowing very little and I am sorry for not asking 'proper' question. Even if the answer is the 'same nonsense' as my question, I am grateful for those who have tried to help me. – denniss Aug 06 '10 at 16:46

4 Answers4

99

Yes, simply set it to another value:

$_POST['text'] = 'another value';

This will override the previous value corresponding to text key of the array. The $_POST is superglobal associative array and you can change the values like a normal PHP array.

Caution: This change is only visible within the same PHP execution scope. Once the execution is complete and the page has loaded, the $_POST array is cleared. A new form submission will generate a new $_POST array.

If you want to persist the value across form submissions, you will need to put it in the form as an input tag's value attribute or retrieve it from a data store.

ADTC
  • 8,999
  • 5
  • 68
  • 93
Sarfraz
  • 377,238
  • 77
  • 533
  • 578
  • and will this stay when I click another submit button without setting it to a hidden field? – denniss Aug 05 '10 at 18:57
  • @denniss: It will override again if a field is named `text` when you submit the form. Why not use a session probably if you want to preserve a certain value. – Sarfraz Aug 05 '10 at 18:58
  • true, i just wanted to know if there is a way to do it with POST – denniss Aug 05 '10 at 19:00
  • @denniss: Right no problem :) – Sarfraz Aug 05 '10 at 19:04
  • 34
    While this answers the literal question, it totally misses the point that doing this is pretty pointless. Yes, you can overwrite values in `$_POST`. Great. It's the same as if you'd do `$foo = $_POST; $foo['bar'] = 'baz';` You are changing a variable, nothing more, nothing less. It does not mean you are altering the HTTP POST data or that this will stick until the next page or that there was any point to the question to begin with. -1 – deceze Aug 31 '12 at 11:09
  • 14
    @deceze: There are legitimate uses for this—for example, setting a `$_POST` variable within a unit test. +1 – Nathan Arthur Jun 11 '14 at 15:16
  • @ deceze Is right to point this out. Trying to "override" $_POST is mostly not useful, by the time you reach the point where the "overridden" value would be used $_POST has been completely reconstructed. Example from Drupal: if you try to mimic the usual form button submission process by hacking $_POST, by the time drupal_build_form($form_id, &$form_state) the form_state is affected by the actual server $_POST, not the one you (think you) manipulated. – Webel IT Australia - upvoter Feb 25 '15 at 10:51
  • @ Nathan Arthur The point about manipulating $_POST for unit tests is not wrong, but in some (many) cases there are better ways. There is a lot one can do with Selenium WebDriver and HTTP header manipulation tools that enables robust role-playing with $_POST, rather than low-level PHP $_POST manipulation, it is basically, in any terms, a hack. – Webel IT Australia - upvoter Feb 25 '15 at 10:53
  • The only scenario when I would manipulate $_POST outside of a form would be when I want to do unit testing without having to submit complex long forms. – Erick Sep 09 '15 at 14:33
  • 1
    Setting a $_POST value is also useful for setting a default when the code expects the $_POST value later in the code. Doing something like `if (!isset($_POST["underwear_ordered_with_tuxedo"])){$_POST["underwear_ordered_with_tuxedo"] = "super_wedgie_thong";}` when later in associated or included codeblocks you have a `order_from_catalog($_POST["underwear_ordered_with_tuxedo"]);` and `share_the_order_on_facebook($_POST["underwear_ordered_with_tuxedo"]);` and more later. – lilHar Feb 01 '16 at 23:28
  • @deceze. A valid scenario is using a form to gather private customer info, check the date and stock is available, then, if OK, change the POST data and use a php header 307 redirect to send only payment-specific POST data to PayPal, VIA the user's browser, so they can explicitly approve the payment. As noted above, $_POST is only a *copy* of the real POST data. – Patanjali Aug 26 '16 at 08:10
  • @Patanjali Changing anything in the `$_POST` array and then redirecting with `307` does absolutely nothing. The client will only receive the `307` status code, it will not receive the altered `$_POST` data and can thereby also not forward that altered data to Paypal. – deceze Aug 26 '16 at 08:16
  • @deceze. I was addressing your expression '... or that there was any point to the question to begin with.' There IS a point because there are valid scenarios for wanting to change the POST data along the way. That changing $_POST doesn't do that in no way invalidates the usefulness of being able to change POST data. So, the OPs question is still valid, regardless of whether you think so. – Patanjali Aug 27 '16 at 02:20
  • **PLEASE DON'T DO THIS.** I hate debugging code that uses $_POST[foo] just to find that somewhere else the programmer set that in code. I realize there are a few cases (such as unit tests) where this is handy but mostly **it is confusing to your fellow programmers**. Use a session if you need persistence. – zkent Nov 08 '16 at 20:32
7

If you want to set $_POST['text'] to another value, why not use:

$_POST['text'] = $var;

on next.php?

Rakward
  • 1,657
  • 2
  • 18
  • 24
  • 1
    The answer to "why not use" is given precisely by @deceze (with the possible exception of low-level unit testing explained by @ Nathan Arthur). Don't "change" $_POST and then wonder why your change has vanished by the next form context. – Webel IT Australia - upvoter Feb 25 '15 at 11:04
4

you can do it using ajax or by sending http headers+content like:

POST /xyz.php HTTP/1.1
Host: www.mysite.com
User-Agent: Mozilla/4.0
Content-Length: 27
Content-Type: application/x-www-form-urlencoded

userid=joe&password=guessme
bcosca
  • 17,371
  • 5
  • 40
  • 51
1

You can do it using jQuery. Example:

<script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

<script>
    $.ajax({
        url : "next.php",
        type: "POST",
        data : "name=Denniss",
        success: function(data)
        {
            //data - response from server
            $('#response_div').html(data);
        }
    });
</script>
joan16v
  • 5,055
  • 4
  • 49
  • 49
  • 11
    You could probably, if you are keen enough, get a web page to sing a Billy Joel song in jQuery, but it doesn't mean it's the best or most elegant way to do it. jQuery is great, but unfortunately it offered the world yet another hundred ways to abuse JavaScript. EDIT: the original question asked "Is there a way to set a $_POST['var'] without using form related field (no type='hidden') and using only PHP" It did not ask "is there yet another way to do something in jQuery that could be done better other ways" – Webel IT Australia - upvoter Feb 25 '15 at 10:57