0

I am having a little bit of trouble coding a registration form. The text fields are no problem, its when I have a selection of checkboxes asking why they chose to register with this particular website.

There are 3 checkboxes and 3 fields in the mysql table for them, the field names are 'interest_1' 'interest_2' 'interest_3'

If the user selects just one check box then the value of that will be inserted into 'interest_1' and so on...

I am really struggling however to create the php and sql code to insert this into the table

Any help would be greatly apprecieted

The checkboxes are

<tr>
<td class="typeMusic">What are your interests:</td>
<td class="checkbox"><input type="checkbox" name="newMusic" value="1"/>To find new and exiting music</td>
<td class="checkbox"><input type="checkbox" name="newMusic" value="2"/>Shopping for music related products</td>
<td class="checkbox"><input type="checkbox" name="newMusic" value="3"/>Other</td>
</tr>
4
Contributors
9
Replies
10
Views
6 Years
Discussion Span
Last Post by lit108
0

All of your checkboxes have the same name attribute, which will cause problems when you're trying to determine what was checked. Try giving each checkbox a unique name, even if it is something like "cb1", "cb2", etc. And give each of them a value of "1".

Then when you process the form, you can assign each checkbox to a variable, and if the variable is equal to one, then insert that checkbox's reason into your database as a string.

Can we see some PHP code in order to see what issue PHP is having?

0

I hate to disagree, but the names are ok - in principle, but I agree, not as they are. What you can do is provide a key for each name.

<td class="checkbox"><input type="checkbox" name="newMusic['new']" value="1"/>To find new and exiting music</td>
<td class="checkbox"><input type="checkbox" name="newMusic['shop']" value="2"/>Shopping for music related products</td>
<td class="checkbox"><input type="checkbox" name="newMusic['other']" value="3"/>Other</td>

Your $_POST variable is now an array (if at least one checkbox is checked).

However, for a trivial form like this with just 3 checkboxes, you may find it easier to rename them to a simple unique name, as TySkby suggests.

0

sorry the name attribute shouldn't be the same that was a typing error. What I have is a look up table and the value from each of the checkboxes corresponds to a value in that lookup table. So for example if only checkbox 2 was clicked, it has the value 2, this value would be added to the 'interest_1' field. I can then call on that lookup table to retrieve the field also with the id of 2

TySkby, I havn't got any php code written yet for inserting these checkboxes because I had no idea where to start

0

Maybe a better way to do this is have one interest field and have your checkboxes with the values; 1,2,4 (bit). So any combination will give a unique value total: 1,2,3,4,5,6,7. e.g. 6 = 2 + 4 (the second and third checkbox).

You can check the presence of a value in the total with a bitwise operator (&):

$a & $b (will give 0 if false or the common bit if true)

2 & 3 = 2 (2 is the common bit)
1 & 3 = 1 (1 is the common bit)
1 & 6 = 0 (no common bits)

Perhaps I haven't explained this very well, and it's possible that it's overcomplicating the solution.

The reason I mentioned this is that if you do this:

<td class="checkbox"><input type="checkbox" name="newMusic[]" value="1"/>To find new and exiting music</td>
<td class="checkbox"><input type="checkbox" name="newMusic[]" value="2"/>Shopping for music related products</td>
<td class="checkbox"><input type="checkbox" name="newMusic[]" value="4"/>Other</td>

SO for processing:

$total = array_sum($_POST['newMusic']);

Now you don't need to check each individual checkbox.

You're all welcome to stomp on my idea. Just a thought.

Edited by diafol: n/a

0

Okay, so if I am understanding correctly, you have two tables- one that stores things like "To find new and exiting music", "Shopping for music related products", etc. each with a corresponding ID key? And the second table is the user information and includes three fields for holding the ID keys that correspond to the first table?

If that's correct, then what you need is for your checkboxes to be enclosed within <form></form> tags with proper attributes, like this:

[B]<form method='post' action='process.php' name='myform'>[/B]
<tr>
<td class="typeMusic">What are your interests:</td>
<td class="checkbox"><input type="checkbox" name="new" value="1"/>To find new and exiting music</td>
<td class="checkbox"><input type="checkbox" name="shop" value="2"/>Shopping for music related products</td>
<td class="checkbox"><input type="checkbox" name="other" value="3"/>Other</td>
</tr>
[B]</form>[/B]

where process.php is the name of the PHP file that the form will send its data to and myform is whatever name attribute the form should have (it usually doesn't matter).


Process.php should look something like this:

<?php

//Retrieve your form data and store it in corresponding variables
$chk_new = $_POST['new'];  //If checked, will equal 1
$chk_shop = $_POST['shop']; //If checked, will equal 2
$chk_other = $_POST['other']; //If checked, will equal 3

//Decide which ones were checked and then store them to DB
if ($chk_new == 1) {
     //...MySQL query here for what to do if the box was checked
}
if ($chk_shop == 2) {
     //...MySQL query here for what to do if the box was checked
}
if ($chk_other == 3) {
     //...MySQL query here for what to do if the box was checked
}

?>

But as you can see, the value attribute doesn't really matter unless you plan on using one of the variables within your query. It would be much simpler to have the value attributes all set to 1, and then your "if" statements will look like this:

//Decide which ones were checked and then store them to DB
if ($chk_new == 1) {
     //...MySQL query here for what to do if the box was checked
}
if ($chk_shop == 1) {
     //...MySQL query here for what to do if the box was checked
}
if ($chk_other == 1) {
     //...MySQL query here for what to do if the box was checked
}

?>

The difference is simplicity- in this last example, you are evaluating whether a box was checked as 1 or nothing (True or False). Your queries should hold the specific stuff, where the query in each "if" statement specifies the field to update, and the value you want to update it with.

Example:

mysql_query("UPDATE table_name SET interest_1='1' WHERE id='$user_id'");

Where table_name is (obviously) the name of the table you're storing the form data to and id='$user_id' is whatever you are using to identify the record you want to update.

Edited by TySkby: n/a

0

Didn't see this before I posted my solution:

Maybe a better way to do this is have one interest field and have your checkboxes with the values; 1,2,4 (bit). So any combination will give a unique value total: 1,2,3,4,5,6,7. e.g. 6 = 2 + 4 (the second and third checkbox).

You can check the presence of a value in the total with a bitwise operator (&):

$a & $b (will give 0 if false or the common bit if true)

2 & 3 = 2 (2 is the common bit)
1 & 3 = 1 (1 is the common bit)
1 & 6 = 0 (no common bits)

Perhaps I haven't explained this very well, and it's possible that it's overcomplicating the solution.

The reason I mentioned this is that if you do this:

<td class="checkbox"><input type="checkbox" name="newMusic[]" value="1"/>To find new and exiting music</td>
<td class="checkbox"><input type="checkbox" name="newMusic[]" value="2"/>Shopping for music related products</td>
<td class="checkbox"><input type="checkbox" name="newMusic[]" value="4"/>Other</td>

SO for processing:

$total = array_sum($_POST['newMusic']);

Now you don't need to check each individual checkbox.

You're all welcome to stomp on my idea. Just a thought.

I agree that it would work, but it looks a bit complex for a trivial checkbox form. Plus the math would have to change if the form was ever updated to include more checkbox options.

0

> it looks a bit complex for a trivial checkbox form

Yes thought so. I think the maths should be OK if checkboxes were added but not amended/deleted. I had expansion in mind.

0

thankyou TySkyb your method made the most sense to me and I worked a solution that follows closely to what you suggested. Thanks alot

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.