1

I am making a website that will show night club revelers events and night establishments in a big city.

A user should be able to add certain details when adding their event on the site e.g.(in the events table) event_id, event_name, event_description, event_date, event_photo_url etc.

I want users to be able to search or find certain events or establishments based on interests. I have an interest table that looks like this.

interest_id, interest-name
1, Shoot Pool
2, Karaoke
3, Lounge
4, Live Band
5, Dancing
6, Watch Sports

So there are 6 checkboxes on my form. A user can select none, 1, 2 or all 6.

Should I have each checkbox submit individual values to my submit form, ie

input name="dancing" type="checkbox" value="5" 
input name="watch_sports" type="checkbox" value="6"

Or should I build an array

input name="interest[]" type="checkbox" value="5" 
input name="interest[]" type="checkbox" value="6" 

I have a table to link events with interests ie;

event_interest table  
event_interest_id            
event_id              
interest_id 

I have never implemented a series of check boxes before. Does anyone know the way to go about capturing user input and storing the values in my database?

4
  • 1
    Do you have any code built so far? If so, post that here for us. You should also go Accept a few Answers from your previous Questions.. Commented Feb 16, 2011 at 18:38
  • @jnpcl. Unfortunately, no code so far. Commented Feb 16, 2011 at 18:50
  • 1
    Basically, if the checkboxes are for similar things, use an array of them. If they're not, then don't :-) Also, if you are going to serialise (serialize for you Americans ;-)) the checkbox data, it's much easier to use arrays of checkboxes. Also, just in case you didn't know, you can reference checkbox arrays by keys, like this: <input type="checkbox" name="boxArray[name]"> If you knew already, my apologies for insulting your intelligence. Commented Feb 16, 2011 at 18:56
  • No problem! I aim to please... ;-) Commented Feb 16, 2011 at 21:19

1 Answer 1

1

you will get as POST or GET those checkbox values which are checked.

so u can do something like:

$interests = $_POST['interests'];
$event_id = intval($_POST['event_id']);//or where ever you get that from
foreach($interests as $interest){
    ....
    $interest = intval($interest);
    $query = "INSERT INTO event_interests  SET interest_id = $interest, event_id = $event_id";
    .... //send query to db
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the reply. So I should use the array method for this. This helps a lot, thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.