Solarpitch Posted January 20, 2009 Share Posted January 20, 2009 Hi, I have a list of check boxes on my page as follows... these are generated dynamically from a database table... <input type='checkbox' name='FM' value='FM'>Full Member<br> <input type='checkbox' name='JM' value='JM'>Junior Member<br> <input type='checkbox' name='LM' value='LM'>Lady Member<br> <input type='checkbox' name='MM' value='MM'>Make Member<br> <input type='checkbox' name='OM' value='OM'>Outside Member<br> What I need to do is catch all the boxes that were selected when the form submits and hold them in an array. Because I then need to do a query from the members table to return the emails of the selected member types. So if the member types LM, OM and JM were selected and stored in an array. I would need to do this query... SELECT email FROM members WHERE member_type = '$member_type' Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/ Share on other sites More sharing options...
Solarpitch Posted January 20, 2009 Author Share Posted January 20, 2009 I actually figured it out. Quite easy. $check=$_POST['check']; while (list ($key,$val) = @each ($check)) { echo "$val,"; } Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/#findComment-741099 Share on other sites More sharing options...
Solarpitch Posted January 20, 2009 Author Share Posted January 20, 2009 Actually... rather than opening a new topic.. how can I determine the last element in the array so I can have the last result without a comma? something like this... <?php $check=$_POST['check']; while (list ($key,$val) = @each ($check)) { echo "$val,"; if($last) { echo "$val"; } } ?> Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/#findComment-741119 Share on other sites More sharing options...
redarrow Posted January 20, 2009 Share Posted January 20, 2009 i dont get you, the value you want, shouldn't have a comma at all i fort. get rid off all commas <?php $check=$_POST['check']; while (list ($key,str_repalce(',','',$val)) = @each ($check)) { echo "$val,"; } ?> Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/#findComment-741125 Share on other sites More sharing options...
cytech Posted January 20, 2009 Share Posted January 20, 2009 Hey solar, Normally how I do this type of stuff is like this: <input type='checkbox' name='member_type[]' value='FM'>Full Member<br> <input type='checkbox' name='member_type[]' value='JM'>Junior Member<br> <input type='checkbox' name='member_type[]' value='LM'>Lady Member<br> <input type='checkbox' name='member_type[]' value='MM'>Make Member<br> <input type='checkbox' name='member_type[]' value='OM'>Outside Member<br> So now, any element that the user selects will go automatically into the array member_type At this point you can loop through it: $types = $_POST['member_type']; // array of values selected by the user foreach($types as $key=>$val){ $types_search.=$val.", "; } // heres a trick to handling that last comma $types_search = substr($types_search, 0, (strlen($types_search)-2)); // removse the last two spots in the string echo $types_search; You could also do something like this: $last_element_pointer = count($types); // last element foreach($types as $key=>$val){ if($last_element_pointer == $key){ // last element echo $val; }else{ echo $val.", "; } } Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/#findComment-741129 Share on other sites More sharing options...
Solarpitch Posted January 20, 2009 Author Share Posted January 20, 2009 Thanks for that cytech. Informative answer! Link to comment https://forums.phpfreaks.com/topic/141590-solved-submitting-checkboxes/#findComment-741137 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.