Re: checkboxes names & values in mySQL (newbie)

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Here's a nifty trick.

Name your check boxes like this.

<input type="checkbox" name="myarray[]" value="value1" />
<input type="checkbox" name="myarray[]" value="value2" />
<input type="checkbox" name="myarray[]" value="value3" />
<input type="checkbox" name="myarray[]" value="value4" />

Then when it posts back to php, you will have a nice array. Lets say that 
they tick all but value2, then you will have:

$myarray == array("value1", "value3", "value4");

I guess the order in which they appear is arbitrary, but that makes no 
matter for a MySQL enum. So just do an implode on the array to put it in 
the database.

Cheers,

Owen Prime
http://www.noggin.com.au



John wrote:

> I'm new at using PHP and mySQL, so please forgive my potentially naive
> question.
> 
> I have a series of checkboxes with different values.  The user can select
> any number of checkboxes, and I need to store this information in a mySQL
> table.
> 
> 2 questions:
> Should I have a unique form name for each checkbox, and store it's boolean
> value in the database, OR (is it more streamlined) ..
> To create an array <input type=checkbox name=apps value =myarray[$i]> with
> the same name but different value for each checkbox.
> 
> Finally, how do I construct the query to store the array of values?  Do I
> need a separate column (enum('n','y') for each value, or should I store
> all the true values as a long text string?
> 
> If anyone has some code snippets, that would help very much.
> 
> Thanks very much!


-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux