Jump to content

[SOLVED] Submitting Checkboxes?


Solarpitch

Recommended Posts

 

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

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";
}
} 
?>

 

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.", ";
    }
}

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.