Jump to content

Posting arrays


tjohnson_nb

Recommended Posts

I often post arrays using html like this;

<td><b>Select This Product</b><input type='checkbox' name='product[13]' value='550.92'></td>

Then I use this code to assign local variable names to the posted variables in the $_POST global;

 

foreach ($_POST as $key=>$value) {
	$$key=$value;
}

 

I recently upgraded a server and the code above no longer works if an element of $_POST is an array as above. I would normally expect to have an array $product after this ran but I get nothing. I even tried explicitly assigning $product=$_POST['product'] and it didn't work. The only way I can access the info is like this;

 

foreach ($_POST['product'] as $key=>$value) {
   [some code]
}

 

Is this a result of upgrading php?

Link to comment
https://forums.phpfreaks.com/topic/145742-posting-arrays/
Share on other sites

Well for a start, why are they two $ in front of $key?

 

Well suppose you have;

 

<td><b>Select This Product</b><input type='text' name='foo' value='bar'></td>

 

Then this code will create the variable $foo="bar";

 

foreach ($_POST as $key=>$value) {

      $$key=$value;

}

Link to comment
https://forums.phpfreaks.com/topic/145742-posting-arrays/#findComment-765186
Share on other sites

Well for a start, why are they two $ in front of $key?

 

That's called a variable variable. That is, the variable that you are using is itself variable. In this case, the OP is using it to extract all of the data from the $_POST array - indeed, the code is equivalent to extract($_POST);

 

As for the original question - could we see some actual code? There's no reason why that shouldn't be working, so perhaps there's something else amiss.

Link to comment
https://forums.phpfreaks.com/topic/145742-posting-arrays/#findComment-765188
Share on other sites

As for the original question - could we see some actual code? There's no reason why that shouldn't be working, so perhaps there's something else amiss.

 

I did post the code  :) That's all there is. I was thinking of trying extract() to see if that worked but the code works for regular variables but it stopped working for array variables. I don't think it is a php5 issue since it works on a linux server with php5 but this is on wampserver where it stopped working.

Link to comment
https://forums.phpfreaks.com/topic/145742-posting-arrays/#findComment-765197
Share on other sites

Well for a start, why are they two $ in front of $key?

 

That's called a variable variable. That is, the variable that you are using is itself variable. In this case, the OP is using it to extract all of the data from the $_POST array - indeed, the code is equivalent to extract($_POST);

 

As for the original question - could we see some actual code? There's no reason why that shouldn't be working, so perhaps there's something else amiss.

 

Many thanks :)

Link to comment
https://forums.phpfreaks.com/topic/145742-posting-arrays/#findComment-765302
Share on other sites

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.