Forum Moderators: coopster

Message Too Old, No Replies

Help needed with checkboxes!

How to parse checkbox values into tables!?

         

toltec75

10:08 pm on Aug 24, 2004 (gmt 0)

10+ Year Member



I am a kind of a n00b at PHP so help is needed desperately!
What I need to do is to insert data from checkbox forms into a table so that for each value parsed from a checkbox a new row will be created in the MySql table that will contain the checkbox value and another value such as user_name for instance.

Here is part of the code!
...
<input type="checkbox" name="music[]" value="4">

<input type="checkbox" name="music[]" value="5">

<input type="checkbox" name="music[]" value="6">
...

So I already have the brackets[] with music and the values go from 1 to 10!

Along with the checkboxes I have text input field named user_name!

Can somebody please write a piece of code that will do what I wrote above? - add the value of each checked checkbox into a new row within a table together with a user_name value!

Please I need you help! :)

Thank you! :)

Birdman

5:32 am on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello,

By adding brackets, you made the form variable an array. To access the array, use a loop.

if (isset($_POST['music'])) {

foreach ($_POST['music'] as $val) {

// RUN DB INSERT CODE HERE USING $val

}

}

toltec75

12:49 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



Thanx man, it works just fine!

You have my eternal gratitude! :)

coopster

7:22 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



And welcome to WebmasterWorld [webmasterworld.com], toltec75!

toltec75

9:24 pm on Aug 25, 2004 (gmt 0)

10+ Year Member



thanx! :)

dreamcatcher

11:31 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome toltec75

:)