Jump to content

Adding to $_POST array is not reflected in $_REQUEST array


snype

Recommended Posts

Hey guys. Just got a quick question that I sure someone has the answer to.

 

Recently I wrote this:

<? echo 'action = '.$_REQUEST['action'].'<br />'; ?>

<?php $_POST['action'] = 'newmember_split'; echo 'action = '.$_REQUEST['action'].'<br />'; ?>

 

My expected output was:

action =

action = newmember_split

 

However my output was:

action =

action =

 

How is this so. As far as I know $_REQUEST is supposed to reflect both $_POST and $_GET. I tried to find some information on it but failed.

 

Can any of you cats help me out?

 

$_REQUEST An associative array consisting of the contents of $_GET, $_POST, and $_COOKIE.

 

in your question logically there request should have a value but (dont know how to explain it properly) request is being set once the value of post data and get  is pass true other page(form action or link using query string ) not by setting it literally

 

sample

when you refresh the page and you see the warning saying about postdata for sure your request will be set.

The $_REQUEST array will only pick up on variables once a request is made. If you change your code too...

 

<?php

  echo 'action = ' . $_POST['action'];
  $_POST['action'] = 'newmember_split';
  echo 'action = ' . $_POST['action'];

?>

 

You will see the changes.

The $_REQUEST array will only pick up on variables once a request is made. If you change your code too...

 

<?php

  echo 'action = ' . $_POST['action'];
  $_POST['action'] = 'newmember_split';
  echo 'action = ' . $_POST['action'];

?>

 

You will see the changes.

i guess that was the thread starter trying to say

his confuse because he thought that once the post is set the request should also be set

 

eg..

$_POST['x']='test';

echo $_POST['x'];// test

echo $_REQUEST['x'];// <-- he expect this to echo  test also

Ok so the $_POST and $_REQUEST Arrays are constructed before this code is invoked. Hence they will not equal each other if a value is changed in one.

 

I get this now. Cheers. But is there a function or process in php to do this? IE postAdd('action', 'newmember_split') which would update all relevant arrays, or would I need to create a custom function to do this?

ut is there a function or process in php to do this? IE postAdd('action', 'newmember_split') which would update all relevant arrays, or would I need to create a custom function to do this?

 

Why on earth would you need to add data to the $_POST array in the first place? Something sounds illogical.

No there isn't a function to do this, but you could rewrite the $_REQUEST superglobal to be a reference to $_POST, but then you lose all the information from the $_GET inside it.

Really there should be NO reason under the sun to do this, but what the hey, here's the code so you can mess your scripts up and then ask why they don't work as expected ;)

 

$_REQUEST =& $_POST;

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.