Jump to content

Automatically tick checkbox depending on what user has clicked


dc_jt

Recommended Posts

Hi

I have a list of categories.

If a user clicks a certain category, they are taken to a form where they can add a listing to that category.

In this form, all categories are listed as checkboxes for example hotels, bars, restaurants.

If the user selected the hotel category orginally, how can the hotel checkbox be automatically ticked, representing this categofy they are in?

Thanks
Name all the checkboxes the same and then loop through them to see what ones have been checked.
Name them all something like this

[code]
<INPUT TYPE='checkbox' NAME='cat[]' value='hotel'>
<INPUT TYPE='checkbox' NAME='cat[]' value='bars'>
<INPUT TYPE='checkbox' NAME='cat[]' value='restaurants'>
[/code]

This will put all the checkboxes into an array when form is submitted, array name being cat.
On the page where you want the checkbox ticked you just use a for loop.

[code]

if(in_array("hotel",$cat)) {
    $hotel_check = "checked";
}else{
    $hotel_check = "";
}

if(in_array("bars",$cat)) {
    $bars_check = "checked";
}else{
    $bars_check = "";
}

if(in_array("restaurants",$cat)) {
    $restaurants_check = "checked";
}else{
    $restaurants_check = "";
}

[/code]


Then have the checkbox HTML look something like this

[code]

<INPUT TYPE='checkbox' NAME='cat[]' value='hotel' <?PHP echo $hotel_checked; ?>>
<INPUT TYPE='checkbox' NAME='cat[]' value='bars' <?PHP echo $bars_checked; ?>>
<INPUT TYPE='checkbox' NAME='cat[]' value='restaurants' <?PHP echo $restaurants_checked; ?>>

[/code]

This is just from the top of my head and nothing is tested, I'm sure there is a better way to do the if statments but
i'm new. Sorry for not being able to optomized better but like I said I'm a newb :)

Hope I was of some help

Cheers!
Stephen

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.