Jump to content

Trouble getting checkbox on form uploaded to SQL


Dave2136

Recommended Posts

Hi. My form involves a checkbox. Below is the code. The problem is that when I am not sure what to put on the php. I need for people to be able to check one box tow boxes or all three boxes. On the form side  I have this:  type="checkbox" name="formcheck[]" value="C". Not sure what to put on the php side and need some assistance

 

<label style="display: block;  padding-left: 15px;  text-indent: -15px; width:650px;">
<input style="width: 45px;     height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="A" /> Directory Listing </label>    
<label style="display: block;  padding-left: 15px;  text-indent: -15px;width:650px;">
<input style="width: 45px;     height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="B" /> Full page listing </label>     
<label style="display: block;  padding-left: 15px;  text-indent: -15px;width:650px;">
<input style="width: 45px;     height: 45px; padding: 0; margin:0; vertical-align: bottom; position: relative; top: -1px; *overflow: hidden;" type="checkbox" name="formcheck[]" value="C" /> Receive a link to your website </label>

Link to comment
Share on other sites

The form asks pick A pick B or pick C, or...pick any combination of the three. Then if they pick one two or three of the choices then I wanted the php to take the choices and to upload that data to mySql. So if they picked A and C to have my SQL have the letters A and C marked and to have B left blank.

 

 

Link to comment
Share on other sites

As requinix said, $_POST["formcheck"] will be an array of the boxes checked. So in your processing code you could do something like this:

 

if (isset($_POST["formcheck"])) {
  foreach($_POST["formcheck"] as $choice) {
    // At this point, $choice will be A or B or C 
    // So do what you want with it
  }
} else {
  // None of the boxes was checked
}

Link to comment
Share on other sites

Ok I dont have the code you gave me working yet but looking at it it seems as if taht code would be

 

$choice. Now $choice would be either A, B or C right? What I was looking for was to be able to pick more than one letter such as A and B.

 

I have 3 checkboxes so I took them into php by

 

$formcheck1=$_POST['formcheck']['value'];
$formcheck2=$_POST['formcheck']['value'];
$formcheck3=$_POST['formcheck']['value'];

 

I wasnt sure whether to place 'value' as 'name' but it doesnt matter because neither works. Dont I have to bring the formcheck[] array into the php page with the above code first before I use the code you gave me, and then the code you gave... isnt that just for either A or B or C and not for A and B (one example)?

 

 

Link to comment
Share on other sites

Your (original) input fields:

 

<input type="checkbox" name="formcheck[]" value="A" />
<input type="checkbox" name="formcheck[]" value="B" />
<input type="checkbox" name="formcheck[]" value="C" /> 

 

Will allow zero, one or more boxes to be checked. If one or more are checked, an array is posted to your script. If the user checked "A" and "B", the array would be:

 

$_POST['formcheck'][0] = 'A';
$_POST['formcheck'][1] = 'B'

 

If "B" and "C" were checked it would be:

$_POST['formcheck'][0] = 'B';
$_POST['formcheck'][1] = 'C'

 

So, the code I posted, walks through this array looking at the values checked. To make is clearer:

 

$whatDidTheUserCheck = array('A' => false, 'B' => false, 'C' => false);

if (isset($_POST["formcheck"])) {
  foreach($_POST["formcheck"] as $choice) {
    // At this point, $choice will be A or B or C 
    // So do what you want with it
    $whatDidTheUserCheck[$choice] = true;
  }
} else {
  // None of the boxes was checked
}

// This will give you a DEBUG dump of what the user checked
var_dump($whatDidTheUserCheck);

 

Just to be clear, Browsers do NOT post checkboxes that are NOT checked. So, the number of elements in the $_POST['formcheck'] array will be the number of boxes checked. As in my examples above, if the user did NOT check box "C", you will NOT get a field POSTed for "C". That is the reason I have the first IF in there -- if the user does NOT check ANY boxes, the 'formcheck' element of the POST array will NOT even exist.

Link to comment
Share on other sites

What I seem to be having a problem with now is actually getting the data onto mySQL. Originally I had three variables called $formcheck1,$formcheck2, and $formcheck3. When I looked at the code you gave me I saw that the variables where changed to $choice. When I make the variable $choice

 

First of all it only gives one data field not three which might make things harder in the future in terms of Sql searches....

 

and the other thing is that when I add $choice into the data to be entered into mySQl only the last letter checked gets added to the database. So if someone checks A and C, I will see C on the database.

Link to comment
Share on other sites

That code uses a variable called $choice, but ONLY inside the loop. It is used to hold the value from each checkbox, in turn, for only a moment. We use that value to update the array -- $whatDidTheUserCheck -- with TRUE for those boxes that the user checked. Since you have never provided any comments or code indicating how you want to use these values, we have not provided any suggestions on how to do it. There are many, many, many, different ways to use the value(s).

 

Another way to do it, and I'm sure this is not the best way:

$userCheckedA = false;
$userCheckedB = false;
$userCheckedC = false;

if (isset($_POST["formcheck"])) {
  foreach($_POST["formcheck"] as $choice) {
    if ($choice == 'A') $userCheckedA = true;
    if ($choice == 'B') $userCheckedB = true;
    if ($choice == 'C') $userCheckedC = true;
  }
} else {
  // None of the boxes was checked
}

Link to comment
Share on other sites

Oh my God. So sorry! I was expecting a message in my inbox saying that you wrote. I didnt know that you wrote to me so I did not respond. I have since looked at what you say, but I have worked it out another way. I am not using the array. I cant wrap my head around array. Instead of one variable to equal my three checkboxes $choice, I now have just labeled each checkbox a completely different name, so now it is just like any other input function. Now I can make three separate input fields on MySql for data input. If some one picks A, B , C or all three it doesnt matter because each field has nothing to do with each other field. This is a very decent solution to my query and I think I am happy with it. Thank you for all your help and sorry again for not responding last week.    :)

Link to comment
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.