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
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
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
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
Share on other sites

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.