1

I'm building a form using HTML with JQuery mobile so that the form can be used on mobile devices.

I have the form exporting to CSV via email. However the write to the CSV file doesn't occur when Checkboxes aren't checked.

Can I use functions in jQuery to pull the values from the checked checkboxes, using the values from the label, and to mark the unchecked boxes as Null or as a Space so that when I import them into Excel it notices the values aren't checked?

<label for="question4" class="input"> 4. What language (s) are you currently using? </label>
<fieldset data-role="controlgroup">
    <legend>Multi select:</legend>
    <input type="checkbox" name="checkbox-1" id="checkbox-1" class="custom" value="english"/>
    <label for="checkbox-1"> English</label>
    <input type="checkbox" name="checkbox-2" id="checkbox-2" class="custom" />
    <label for="checkbox-2">French</label>
    <input type="checkbox" name="checkbox-3" id="checkbox-3" class="custom" />
    <label for="checkbox-3">Spanish</label>
    <input type="checkbox" name="checkbox-4" id="checkbox-4" class="custom" />
    <label for="checkbox-4">Brazilian Portuguese</label>
    <input type="checkbox" name="checkbox-5" id="checkbox-5" class="custom" />
    <label for="checkbox-5">Italian</label>
    <input type="checkbox" name="checkbox-6" id="checkbox-6" class="custom" />
    <label for="checkbox-6">German</label>
    <input type="checkbox" name="checkbox-7" id="checkbox-7" class="custom" />
    <label for="checkbox-7">Japanese</label>
</fieldset>
</br>

if there isn't a way to do in JQuery, what about in PHP? Since I'm using PHP to populate the CSV file.

Would it be some form of a if statement that says:

if checkbox = yes then pull value from <label>
Paulo Tomé
  • 1,910
  • 3
  • 18
  • 27
user1292136
  • 13
  • 1
  • 1
  • 3

3 Answers3

8

Change your HTML and give a name attribute to all your checkboxes, like so:

<form action="" method="post">
<label for="question4" class="input"> 4. What language (s) are you currently using? </label>
<fieldset data-role="controlgroup">
<legend>Multi select:</legend>
<input type="checkbox" name="checkbox[]" id="checkbox-1" class="custom" value="english"/>
<label for="checkbox-1"> English</label>
<input type="checkbox" name="checkbox[]" id="checkbox-2" class="custom" />
<label for="checkbox-2">French</label>
<input type="checkbox" name="checkbox[]" id="checkbox-3" class="custom" />
<label for="checkbox-3">Spanish</label>
<input type="checkbox" name="checkbox[]" id="checkbox-4" class="custom" />
<label for="checkbox-4">Brazilian Portuguese</label>
<input type="checkbox" name="checkbox[]" id="checkbox-5" class="custom" />
<label for="checkbox-5">Italian</label>
<input type="checkbox" name="checkbox[]" id="checkbox-6" class="custom" />
<label for="checkbox-6">German</label>
<input type="checkbox" name="checkbox[]" id="checkbox-7" class="custom" />
<label for="checkbox-7">Japanese</label>
</fieldset>
</br>
<input type="submit" name="submit">
</form>

Once the form has been submitted, the name attributes will be passed as array and they'll be accessible using the same variable. Now, you can use isset() to check if a particular item was set:

if(isset($_POST['checkbox'])) {
  print_r($_POST); //print all checked elements
}

If you want to get the number of checkboxes that were checked, you can use count():

$number = count($_POST['checkbox']);

Note: currently, only the first element has a value attribute. You'll have to add it for the rest of the checkboxes for it to work properly.

Hope this helps!

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • This is really helpful! I wanted to know if there is a way of me not having to add the name and just use what I've got? – user1292136 Aug 22 '13 at 14:37
  • @user1292136: Of course you can. This is just the recommended practice, but if you want it that way, surely it can be done. Something like `if(isset($_POST['checkbox-1']) { ... }` should work. – Amal Murali Aug 22 '13 at 14:42
  • that's fantastic! and will this pull the value of the label? so if i have "english,italian and japanese" selected would it then print english,italian,japanese? – user1292136 Aug 22 '13 at 14:43
  • @user1292136: yes, it will get `value` attribute. In your HTML above, only `English` has a value attribute. You'll have to add it for the rest of the elements and it'll work as you described. – Amal Murali Aug 22 '13 at 14:45
  • ah! is there no way of getting it from the – user1292136 Aug 22 '13 at 14:51
  • @user1292136: it's possible with JavaScript, but using `value` is the recommended way to go. See [this](http://stackoverflow.com/questions/7012509/how-to-access-value-of-a-label-in-php) question. – Amal Murali Aug 22 '13 at 14:54
2
  foreach($_POST['checkbox'] as $value)
  {
     echo 'checked: '.$value.'';
  }
Deepanshu
  • 21
  • 1
0

Have a quick look at this answer for checking if a checkbox is checked.

How to check whether a checkbox is checked in jQuery?

But basically you want to do something like below to check its value:

if ($("#element").is(":checked")) {
  alert("I'm checked");
}
Community
  • 1
  • 1
Mark
  • 2,184
  • 1
  • 16
  • 28
  • Thanks for your super fast reply! So using that if statement, would I have to do a loop to count how many checkboxes are checked? Could I do a universal command to go through the entire form and then input that into a .csv , I think I'm getting confused now. – user1292136 Aug 22 '13 at 14:23
  • Take a look at the official documentation here: http://api.jquery.com/checked-selector/ But basically have a look at my jsfiddle here, it should do what you want: http://jsfiddle.net/fdCba/ – Mark Aug 22 '13 at 14:26
  • Thank you so much this, along with the answer above has been so helpful! – user1292136 Aug 22 '13 at 14:55