Setting POST variable without using form
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.
How to pass a POST variable without a form?
There is a way to distinguish forms using
<input type="submit" name="form" value="form1">
to send data, in that way you can read $_POST['form']
value and check what form it is
If you want to send data without form you can use ajax with Javascript.
Setting $_POST variables
You should either put that variable as an hidden field in your form, or use a session variable.
Hidden field
<form method="POST" action="someactionpage.php">
<input type="hidden" name="my_var" value="<?php echo $myvar; ?>" />
<!-- ... -->
</form>
And get it after in someactionpage.php with $_POST['my_var']
when the form is submitted.
Session variable
Just store it whithin the $_SESSION variable
<?php
session_start (); // Just once at the beginning of your code
// ...
$_SESSION['my_var'] = $myvar;
?>
and retrieve it on another page with
<?php
session_start (); // Same than before
// ...
echo $_SESSION['my_var'];
?>
Additional info
As pointed out in some answers and comments, you should always check that the variable is present, because you have no guarantee of that. Just use the isset function
if (isset ($_SESSION['my_var']))
// Do stuff with $_SESSION['my_var']
or
if (isset ($_POST['my_var']))
// Do stuff with $_POST['my_var']
As pointed out by Kolink in the comments, a field value (sended via POST) can be easily seen and changed by the user. So always prefer session variables unless it is really non-critical info.
PHP - $_POST variable is not set after form submitting
I'll just post it as an answer so anybody having the same issue can find it easily.
I just solved it myself by changing $_POST by $_REQUEST even I still don't know why the other way wouldn't work...
Edit: after checking deeper into it, I've seen with Chrome that the request sent was a GET request while it should have to be a POST. So finally I've fixed it changing the action from action="soapclient.php"
to action="/soapclient.php"
and now it sent a POST and the variable $_POST had the value of the username.
Submit form on same page without reloading and use POST variable
Your code runs fine on my server. Maybe you aren't totally clear on the function of the superglobals.
If the "result" div contains "1" after you press the button, then that means process.php is correctly receiving your POST request and echoing back the value of $_POST["select"]. You will get "NULL" if you try to just navigate your browser to process.php, because when you do so you are making a separate request which doesn't contain any POST variables. The superglobal arrays don't persist between different calls to process.php unless you create that functionality using $_SESSION, a DB, or some kind of text/json/xml storage system. The following changes to your PHP will allow you to click your button and then separately navigate to process.php and see your data:
<?php
session_start();
if ($_POST["select"]) {
$_SESSION["data"] = ($_POST["select"]);
}
var_dump($_SESSION);
?>
Please correct me if I have made the wrong assumptions and this is not helpful.
-Ben
Sending Variables in Post without the input tag
use <input type="hidden">
Related Topics
What's the Difference Between Is_Null($Var) and ($Var === Null)
Openssl Not Working on Windows, Errors 0X02001003 0X2006D080 0X0E064002
MySQL PHP - Select Where Id = Array()
Check If Laravel Model Got Saved or Query Got Executed
Codeigniter - Correct Way to Link to Another Page in a View
PHP Float Calculation Error When Subtracting
Laravel's 5.3 Passport and API Routes
Error Building:Fatal Error: Pcre.H: No Such File or Directory
Removing Black Borders 4:3 on Youtube Thumbnails
PHP - Count Specific Array Values
Laravel Quick Start Guide Route Not Working
PHP MySQL_Real_Escape_String() -> Stripslashes() Leaving Multiple Slashes
Formatting an SQL Timestamp with PHP