Setting a POST variable without using a form

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

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

Is there any way to do this?

EDIT: Someone asked me to clarify something. So, for example, I have a 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> 

After clicking "Submit" I want to redirect to next.php. Is there a way to set the variable $ _POST ['text'] to a different value? How to make this permanent, so when I click on another submit button (for example), $ _POST ['text'] will be what I set on next.php without using a hidden field.

Let me know if this is not yet clear and thanks for your help.

+45
html post php
Aug 05 '10 at 18:44
source share
4 answers

Yes, just set it to a different value:

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

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

Caution: This change is only visible within the same PHP runtime. After completion and page loading, the $_POST array is cleared. A new $_POST array will be created in the new form view.

If you want to store the value in the form submissions, you will need to put it in the form as an input tag value attribute or get it from the data store.

+62
Aug 05 '10 at 18:55
source share

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

 $_POST['text'] = $var; 

on next.php?

+6
Aug 05 '10 at 18:55
source share

you can do this with ajax or by sending http headers + content, for example:

 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 
+2
Aug 05 '10 at 18:52
source share

You can do this 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
Feb 20 '15 at 11:38
source share



All Articles