Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
184
rated 0 times [  189] [ 5]  / answers: 1 / hits: 72974  / 11 Years ago, thu, august 22, 2013, 12:00:00

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>

More From » php

 Answers
5

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!


[#76210] Wednesday, August 21, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaleyv

Total Points: 259
Total Questions: 99
Total Answers: 107

Location: Saint Helena
Member since Tue, Nov 3, 2020
4 Years ago
;