Jump to content

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.

 

Edited by Zane
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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