简体   繁体   中英

Get check box value if checked

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>

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!

  foreach($_POST['checkbox'] as $value)
  {
     echo 'checked: '.$value.'';
  }

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");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM