EzEApostle
asked on
Form Checkboxes Dynamically returned
Hi Experts,
I have a form which I have a number of checkboxes for as search criteria for cars, rather than manually insert the checkboxes I am pulling them from my database so it returns checkboxes like:-
Vauxhall
Ford
Seat
etc etc
I originally named the checkboxes a value (vehicles) but when returning the results of the selected vehicles it only returns the latest selection.
I have given my checkbox name and values my vehicleid to identify the selections but can anyone help me as to how to get all the id's selected in a built up string with comma's so something like this:-
1,2,3 (1,2,3 being the vehicle id) so I can use this string in my SQL statement.
Thanks
Tim
I have a form which I have a number of checkboxes for as search criteria for cars, rather than manually insert the checkboxes I am pulling them from my database so it returns checkboxes like:-
Vauxhall
Ford
Seat
etc etc
I originally named the checkboxes a value (vehicles) but when returning the results of the selected vehicles it only returns the latest selection.
I have given my checkbox name and values my vehicleid to identify the selections but can anyone help me as to how to get all the id's selected in a built up string with comma's so something like this:-
1,2,3 (1,2,3 being the vehicle id) so I can use this string in my SQL statement.
Thanks
Tim
The code:-
$sql='SELECT vehicle_id, display_name FROM vehicles';
$sqlresult=mysql_query($sql);
$sqlcount=mysql_num_rows($sqlresult);
The checkboxes:-
while($vehiclelist=mysql_fetch_array($sqlresult,MYSQL_NUM))
{
echo '<input type="checkbox" name="'.$vehiclelist[0].'" value="'.$vehiclelistt[0].'" />'.$vehiclelist[1].'<br/>';
}
I'm not quite sure on what you need?! ... do you want the boxes to be checked or do you want to pull out a list with only the selected boxes?
ASKER
On the user filling out the search form I need the ID's (name) of the checkboxes that were selected :) by default they're all unselected
Ok, i think there is a smarter way retrieving the content of the $_post, but the follwing should work
$sql='SELECT vehicle_id, display_name FROM vehicles';
$sqlresult=mysql_query($sql);
$sqlnew='SELECT vehicle_id, display_name FROM vehicles WHERE ';
$firstruntrough=0;
while($vehiclelist=mysql_fetch_array($sqlresult))
{
if($_POST["yourchecknames"]==1){
if($firstrunthrough==0){
$firstrunthrough=1;
$sqlnew.="vehicle_id = $_POST["yourcheckvalue"] ";
} else {
$sqlnew.="OR vehicle_id = $_POST["yourcheckvalue"] ";
}
}
}
ASKER
I dont think that will work because $_POST["yourchecknames"] is set to different ID's i.e. the Vehicle ID
Ok then this should work
$sql='SELECT vehicle_id, display_name FROM vehicles';
$sqlresult=mysql_query($sql);
$sqlnew='SELECT vehicle_id, display_name FROM vehicles WHERE ';
$firstruntrough=0;
while($vehiclelist=mysql_fetch_array($sqlresult))
{
if($_POST["yourchecknames"] != 0){
if($firstrunthrough==0){
$firstrunthrough=1;
$sqlnew.="vehicle_id = $_POST["yourchecknames"] ";
} else {
$sqlnew.="OR vehicle_id = $_POST["yourchecknames"] ";
}
}
}
ASKER
Again, I can't see it working because, this value $_POST["yourchecknames"] isn't a single value but many
I refer to my previous statement:-
echo '<input type="checkbox" name="'.$vehiclelist[0].'" value="'.$vehiclelistt[0]. '" />'.$vehiclelist[1].'<br/> ';
I refer to my previous statement:-
echo '<input type="checkbox" name="'.$vehiclelist[0].'"
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.