Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/68561-checkbox-value/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344647
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344660
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344713
Share on other sites

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      >----+

Link to comment
https://forums.phpfreaks.com/topic/68561-checkbox-value/#findComment-344738
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.