Jump to content

Transferring additional data through $_POST and $_GET


skyrifle

Recommended Posts

Hi,

I am a curious fellow just beginning to use PHP. I understand the basics of what the $_GET and $_POST superglobals do, and how we can use them to retrieve data after form submission. I also know that since $_GET and $_POST are just associate arrays, we can create our own values in both $_GET and $_POST by writing statements like $_POST['variable'] = "value".

I am wondering if it is at all possible to send data beyond form data by adding new key/value pairs into $_GET and $_POST. So for example, if I had a form that transferred username/password through Post, would it be possible for me to include further data by just saying $_POST['formtype'] = "house_insurance_form"?

Basically what I'm getting at is trying to physically add your own data to $_POST or $_GET that you could pull from the next page where the form redirects to. In other words, can I send data beyond form data using these superglobals? I know I can do this with $_SESSION, but could I do it through $_POST or $_GET? I haven't been able to accomplish this, and I was just wondering if someone could give me an in-depth explanation of how this might work or how this is not possible. I would really really really appreciate it. 3b63d1616c5dfcf29f8a7a031aaa7cad.gif

Best Regards,
-Sky

 

 

Basically what I'm getting at is trying to physically add your own data to $_POST or $_GET that you could pull from the next page where the form redirects to

No, any changes made to any superglobals variables (except $_SESSION) in your code will not be remembered.

 

PHP handles the $_SESSION superglobal differently, whereby at the end of script execution it will write the data stored in that array to a file on the server. On the next page PHP will read the contents of that file and populate the contents of the $_SESSION superglobal.

 

The only way to persist data form page to page is either query string parameters in the url, a from (using hidden input fields) or using sessions/cookies.

 

 

 

So for example, if I had a form that transferred username/password through Post, would it be possible for me to include further data by just saying $_POST['formtype'] = "house_insurance_form"?

You could define  formtype  as a hidden input field and give the value of  house_insurance_form  Example:

<form method="post" action="...">
   ...
   <input type="hidden" name="formtype" value="house_insurance_form" />
</form>

POST and GET only contain the values that were sent TO the current script.

Those superglobals are not consistent throughout your pages,  The $_SESSION variable however, is indeed consistent, as long as you call session_start BEFORE any output to the browser.

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.