Jump to content

getting values from variable number of select fields


real_illusions

Recommended Posts

Hi,I have a form that is prepopulated from another page with a variable number of items. Against each item is a select drop down box with a number of options.How do I list the options that are chosen by the user, when the form is submitted, associated with each item.I can add the item name into the option value, but as the item name is unique, I cant add an incrementing number onto the item name, as I need the item name intact.I kinda need a foreach select dropdown box, echo out the selected option, but how do you do that in code?Hope that makes sense.Thanks:)

Link to comment
Share on other sites

well, I would add the item name to the select tag and prefix it with something distinct, like select_. You could then loop through the post array and look for key's whose members begin with select_' (using a regex or string function) and then build an associative array using a string function to remove the select_ prefix, so that you only have the name left, save that as the new key of your new array, and save the value with it. for example,

$prefix = 'select_';$itemOptionMap = array();foreach($_POST as $key){  if($key includes the substring $prefix){	$itemName = $key - $prefix	//pseudo code in place of actual string function	$itemOptionMap[$itemName] = $_POST[$key];  };};

Link to comment
Share on other sites

Not getting anything with this:I dont really get whats happening with the code anyway...$prefix = 'select_';$itemOptionMap = array();foreach($_POST as $key){$findprefix = strpos($key,$prefix); if($findprefix == true){ $itemName = $key - $prefix; //pseudo code in place of actual string function echo $itemOptionMap[$itemName] = $_POST[$key]; };};

Link to comment
Share on other sites

well, I would add the item name to the select tag and prefix it with something distinct, like select_. You could then loop through the post array and look for key's whose members begin with select_' (using a regex or string function) and then build an associative array using a string function to remove the select_ prefix, so that you only have the name left, save that as the new key of your new array, and save the value with it. for example,
$prefix = 'select_';$itemOptionMap = array();foreach($_POST as $key){  if($key includes the substring $prefix){	$itemName = $key - $prefix	//pseudo code in place of actual string function	$itemOptionMap[$itemName] = $_POST[$key];  };};

the select tags need to be given a unique name so that you identify which members/keys in the $_POST are selects, and one way is to add a prefix to the markup. However, since you need to preserve the name, you need to also strip that prefix after the fact. so the gist is:1) the naming convention for your select tags would look something like
<select name="select_itemName1">...</select>

1) loop through all the $_POST members

foreach($_POST as $key)

2) identify which one's are select by scanning each $key to see if it contains the prefix string that you defined, i.e. select_ and make sure it's at the beginninghttp://www.php.net/manual/en/function.stripos.php

if(strpos($key, $prefix) == 0){  //means 'select_' is in the key at the beginning, so this is a select/option association  ..}

3) once we've got a known select/option association, you want to get just the item name

$itemName = substr($key, 7);//7 is the index to start searching after, because that is the value of 'select_' + 1

4) now we want to save the itemName in it's own array, as well as the selection option. So we just initialize an array index with our new name and the option value

$itemOptionsMap[$itemName] = $_POST[$key];

I had intentionally left some pseudo code in there, but this should try and fill in the gaps. Essentially you are creating a new array to hold just the item names and the associated option by targeting specifically named keys from the $_POST array.so our final outcome would look something like this:

$prefix = 'select_';$prefixLength = strlen($prefix);$itemOptionMap = array();foreach($_POST as $key){  echo 'checking key: ' . $key . '<br/>';  if(strpos($key, $prefix) == 0)){	echo 'found a select/option association: ' . $key . ' => ' . $_POST[$key] . '<br/>';	$itemName = substr($key, $prefixLength+1);	$itemOptionMap[$itemName] = $_POST[$key];  };};echo 'all items names and their selected options: <br/>';var_dump($itemOptionMap);

the only thing that may need to change is the last line, I'm not too sure how PHP interprets variables when assigning them to array keys, so you have to change the last line to this:

$itemOptionMap['$itemName'] = $_POST[$key];

or I think this would work

$itemOptionMap[{$itemName}] = $_POST[$key];

depending on what the var dump shows.edit: made a couple of edits about strlen/prefix length. Mixing index and lengths can get a bit confusing...

Link to comment
Share on other sites

Doesn't seem to do how I expected.This is the var dump:

namefound a select/option association: name => nameemail@example.comfound a select/option association: email@example.com =>666found a select/option association: 666 =>messagefound a select/option association: message =>truefound a select/option association: true =>7x5found a select/option association: 7x5 =>A4found a select/option association: A4 =>jpgfound a select/option association: jpg =>found a select/option association: =>all items names and their selected options:array(2) { [0]=> NULL ["ample.com"]=> NULL }
Which is basically echoing out the rest of the form fields (name, phone, email and a message), along with the 3 selected options (7x5, A4 and jpg).The item names are no where to be seen in that var dump.
Link to comment
Share on other sites

if you put this at the top of your script

var_dump($_POST);

what is the output?also. we should add an echo for $itemName after it's been assigned, too.edit: so yeah, let's try this and see what we get. I'm not as familiar with this stuff in PHP as I am in Javascript.

foreach($_POST as $key => $value){  echo 'checking key: ' . $key . '<br/>';  if(strpos($key, $prefix) !== false)){	echo 'found a select/option association: ' . $key . ' => ' . $value . '<br/>';	$itemName = substr($key, $prefixLength+1); 	echo 'itemName is: ' . $itemName . '<br/>';	$itemOptionMap[$itemName] = $value;  };};

PHP loops/array always throw me off a little. I'm more used to Javascript arrays and JSON.

Link to comment
Share on other sites

array(8) { ["name"]=> string(4) "name" ["email"]=> string(17) "email@example.com" ["phone"]=> string(3) "666" ["comments"]=> string(7) "message" ["submitform"]=> string(4) "true" ["select_IMG_4233_JPG"]=> string(3) "7x5" ["select_IMG_4227_JPG"]=> string(2) "A4" ["select_IMG_4218_JPG"]=> string(3) "jpg" }

Link to comment
Share on other sites

array(8) { ["name"]=> string(4) "name" ["email"]=> string(17) "email@example.com" ["phone"]=> string(3) "666" ["comments"]=> string(7) "message" ["submitform"]=> string(4) "true" ["select_IMG_4233_JPG"]=> string(3) "7x5" ["select_IMG_4227_JPG"]=> string(2) "A4" ["select_IMG_4218_JPG"]=> string(3) "jpg" }
well that looks good. Hopefully you don't mind bearing with me as we sort through the foreach loop then :)I'm also thinking subtr values I came up with might be a little off, but that's just a simple tweak once we start getting the right key's and values filtering themselves out.
Link to comment
Share on other sites

yeah thats fine :)how do we filter out the right stuff then?
just got home so decided to put this to task, and it looks like my last code example pretty much nailed most of the desired functionality. The only thing I had to do was remove 1 from the strstr function, and changed the if statement to go back to checking for 0 instead. This is the whole thing with a mock $_POST array, and filtering out only the members with the select_ prefix, and with echo's added for documentation purposes.
<?phpecho 'Object Mapper/String Manipulator Example <br/>';echo '<hr>';$mockPostArray = array("name" => "real illusions", "email" => "email@example.com", "phone" => "555-1234", "comments" => "this is a comment", "submitform" => "true", "select_IMG_4233_JPG" => "7x5", "select_IMG_4227_JPG" => "A4", "select_IMG_4218_JPG" => "jpg");echo 'Mock Post Array: <br>';var_dump ($mockPostArray);echo '<br>';echo '<hr>';echo '<br>';echo 'Filtering. . .<br>';$prefix = "select_";$prefixLength = strlen($prefix);$itemToOptionMap = array();foreach($mockPostArray as $key => $value){  echo 'checking key: ' . $key . '<br/>';  if(strpos($key, $prefix) === 0){	echo '** found a select/option association: ' . $key . ' => ' . $value . '<br/>';	$itemName = substr($key, $prefixLength); 	echo '*** itemName is: ' . $itemName . '<br/>';	$itemToOptionMap[$itemName] = $value;  };};echo '<br>';echo '<hr>';echo '<br>';echo 'Item Name to Option Map: <br/>';var_dump($itemToOptionMap);?>

you can copy/paste this right into your editor and run it to see it working. To apply it, just change the filtering section to use $_POST instead of the mock array.

Link to comment
Share on other sites

what about? Did you run the code to see the output? I was under the impression that you were trying to find distinct $_POST members as being specific to a select tag and a user selecting an option. We are prefixing those form element names with a name we can anticipate on the server side, and then we can target them and get their values and also know they came from a select/option association, while still preserving the real name (without the prefix).the only real functionality is being able to filter it from $_POST, for my example I just made my own version of a $_POST array rather than making a form (just for testing purposes). I just choose to save those specific select/option associations into an object, but you can do whatever you need with it within that filtering functionality.Do you have a specific data structure you need in order to precede with your application after you filtered it? Do you need to merge back into $_POST or do you need to email it or save it to a database? If you want, it would be very simple to marge everything back into one larger object if that would make more since. i.e.

<?php$prefix = "select_";$prefixLength = strlen($prefix);$filteredPostArray = array();foreach($_POST as $key => $value){  echo 'checking key: ' . $key . '<br/>';  if(strpos($key, $prefix) === 0){	echo '** found a select/option association: ' . $key . ' => ' . $value . '<br/>';	$itemName = substr($key, $prefixLength); 	echo '*** itemName is: ' . $itemName . '<br/>';	$filteredPostArray[$itemName] = $value;  }else{	echo '* non-prefixed member, don't modify<br/>';	$filteredPostArray[$key] = $value;  };};var_dump($filteredPostArray);?>

this simple modification will basically give you a filtered $_POST array and basically return to you what $_POST submitted as, except now the members that had the select_ prefix will have them removed and everything else will stay the same.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...