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.

9
  • 3
    You mean in the server code or from the client side when a user access some page? Commented Aug 5, 2010 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. Commented Aug 5, 2010 at 18:46
  • 1
    What? And why do you not want to use a hidden field? That's what they're for. Commented Aug 5, 2010 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. Commented Aug 5, 2010 at 18:55
  • 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. Commented Aug 5, 2010 at 19:08

4 Answers 4

102

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.

Sign up to request clarification or add additional context in comments.

14 Comments

and will this stay when I click another submit button without setting it to a hidden field?
@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.
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: There are legitimate uses for this—for example, setting a $_POST variable within a unit test. +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.
|
8

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

$_POST['text'] = $var;

on next.php?

1 Comment

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.
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

3 Comments

how do you "echo" the data userid=joe&password=guessme ?
parse_str(file_get_contents('php://input'));
This is pretty cool. It seems this can be used as an alternative to CURL to set posted values. Am I correct?
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>

1 Comment

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"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.