Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting POST variable without using form

Tags:

html

post

php

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.

like image 670
denniss Avatar asked Aug 05 '10 18:08

denniss


People also ask

When you use the $_ POST variable?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post".

What is the difference between $post and $_ POST?

$_POST is a superglobal whereas $POST appears to be somebody forgetting the underscore. It could also be a standard variable but more than likely it's a mistake.

What is $_ POST?

$_POST is a predefined variable which is an associative array of key-value pairs passed to a URL by HTTP POST method that uses URLEncoded or multipart/form-data content-type in request.


1 Answers

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.

like image 72
Sarfraz Avatar answered Sep 23 '22 04:09

Sarfraz