Jump to content

Multiple POST variables into an Array


14pulsars

Recommended Posts

Hi Guys, php noobie here:

 

I have a bit of a problem: I need to receive an indeterminate amount of similar POST variables (they are SKU's from a shopping cart). I want to place them in an array for later use and comparison with another array used to print item details based on the SKU.

 

I tried this to place the SKU's in an array, but I get an error on the 'if' line. (unexpected T_VARIABLE, expecting ']')

Obviously, there is a syntax error, but how is the following code invalid? Any suggestions?

 

$sku = array($_POST['sku1']);
for ($i = 2; $i<19; $i++) {
if (isset($_POST[sku$i])) {
	$sku[$i-1] = $_POST[sku$i];
}else{ break; }
}

Link to comment
https://forums.phpfreaks.com/topic/106033-multiple-post-variables-into-an-array/
Share on other sites

Hey, that worked! Thanks.

 

I didn't think about bringing in the first array addition into the loop.

I also did not know you could concatenate inside variables like that. THANKS!

 

I'm going to remember this!

 

Try this:

 

<?php
$sku = array();
for ($i = 1; $i<18; $i++) {
if (isset($_POST['sku'.$i])) {
	$sku[$i] = $_POST['sku'.$i];
}else{ break; }
}
?>

And to make things simpler, you could consider naming the input fields with the SKU data "sku[]". Then $_POST['sku'] contains an array of values from the SKU input fields (= what you're looking for).

 

The problem is that I don't have access to those fields, the data comes from the POST from a third party CC processor. Absolutely no control over field names or anything.

 

Though your suggestion would have been ideal.

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.