angelsRock Posted September 9, 2007 Share Posted September 9, 2007 hi.. if i have 10 checkbox for customer interest.. how do i save that to database?? need to create 10 fields in the database?? after save.. how to display back when customer view his data.. if i choose swimming.. next time when i view back.. how to show swimming in the checkbox being checked Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/ Share on other sites More sharing options...
recklessgeneral Posted September 9, 2007 Share Posted September 9, 2007 Hi, I think probably the easiest way would be to have a field for each interest in your database. As your users are either interested in something or not, I'd set the data type to be a small integer (I use tinyint in mysql), and store 1 if they're interested and 0 otherwise. Next, I imagine your form will look something like the following: <form name="interests" action="interests.php" method="post"> Swimming: <input type="checkbox" name="swimming" value="1"> ... other interests ... </form> When the user submits the form, if they clicked the swimming checkbox, $_POST['swimming'] will be set to "1", otherwise it won't be set. $swimming = (isset ($_POST['swimming']) ? 1 : 0); I have used the ?: syntax, which basically will set $swimming to 1 if $_POST['swimming'] has been set, otherwise $swimming is 0. You would extract all interests in a similar way, and build your query based on those values: $query = "INSERT INTO interests (swimming) VALUES ('$swimming')"; To display the correct state of the checkboxes, you can use the value based in $swimming to generate the "checked" keyword as part of the checkbox field: <form name="interests" action="interests.php" method="post"> Swimming: <input type="checkbox" name="swimming" value="1" <?php if ($swimming == 1) { echo "checked"; } ?> > </form> Hope this helps, Darren. Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344647 Share on other sites More sharing options...
angelsRock Posted September 9, 2007 Author Share Posted September 9, 2007 cool.. but in my page for interest... there are more than 25 checkbox .. if i declared each interest as a field in database.. won it be a eyeshore here...!!! ? this interest is just optional for customer to fill in..!! also.. if i declare it to different fields... then when customer view back his interest...it will be a problem as it from different fields... i was thinking of storing it into an array. (1,,3,,5,,,,,,) something like this/? if i do that .. how do i make the checkbox to be checked if the database consist that value.. really thanks Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344660 Share on other sites More sharing options...
recklessgeneral Posted September 9, 2007 Share Posted September 9, 2007 Hi, I found this article (http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html) that covers what you want to do by using a SET data type and storing all user interests in one field. There are pros and cons using this approach, which are discussed, but maybe fine for what you need. Darren. Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344713 Share on other sites More sharing options...
Barand Posted September 9, 2007 Share Posted September 9, 2007 I'd avoid multiple fields and sets. Better to have separate table, say, customer_interests, with row for each item that they are interested in. [pre] customer cust_interest interest ----------- -------------- ---------- custID --+ id +----- interID cust_name +--< custID | interest interID >----+ Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344738 Share on other sites More sharing options...
darkfreaks Posted September 9, 2007 Share Posted September 9, 2007 try: <?php while (list ($key,$val) = @each ($box)) { echo "$val,"; } ?> <form name="interests" action="interests.php" method="post"> Swimming: <input type="checkbox" name="box[]" value="swimming"> Quote Link to comment https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344768 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.