Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如果选中,则获取复选框值_Php_Javascript_Jquery_Html - Fatal编程技术网

Php 如果选中,则获取复选框值

Php 如果选中,则获取复选框值,php,javascript,jquery,html,Php,Javascript,Jquery,Html,我正在使用JQuery mobile使用HTML构建一个表单,以便该表单可以在移动设备上使用 我通过电子邮件将表格导出到CSV。但是,如果未选中复选框,则不会写入CSV文件 我是否可以使用jQuery中的函数从选中的复选框中提取值,使用标签中的值,并将未选中的框标记为Null或空格,以便在将它们导入Excel时,它会注意到这些值未被选中 <label for="question4" class="input"> 4. What language (s) are you current

我正在使用JQuery mobile使用HTML构建一个表单,以便该表单可以在移动设备上使用

我通过电子邮件将表格导出到CSV。但是,如果未选中复选框,则不会写入CSV文件

我是否可以使用jQuery中的函数从选中的复选框中提取值,使用标签中的值,并将未选中的框标记为
Null
空格
,以便在将它们导入Excel时,它会注意到这些值未被选中

<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>
4。您当前使用的语言是什么?
多选:
英语
法语
西班牙的
巴西葡萄牙语
意大利人
德国的
日本人

如果在JQuery中没有方法,那么在PHP中呢?因为我使用PHP来填充CSV文件

它是否会是某种形式的if声明,表示:

if checkbox = yes then pull value from <label>
如果复选框=是,则从

快速查看此答案,检查是否选中复选框

但基本上,您需要执行以下操作来检查其值:

if ($("#element").is(":checked")) {
  alert("I'm checked");
}

快速查看此答案,以检查是否选中了复选框

但基本上,您需要执行以下操作来检查其值:

if ($("#element").is(":checked")) {
  alert("I'm checked");
}

更改HTML并为所有复选框指定
name
属性,如下所示:

<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>
如果要获取选中的复选框数,可以使用
count()

注意:当前,只有第一个元素具有
属性。您必须将其添加到其余复选框中,才能使其正常工作


希望这有帮助

更改HTML并为所有复选框指定
name
属性,如下所示:

<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>
  foreach($_POST['checkbox'] as $value)
  {
     echo 'checked: '.$value.'';
  }
如果要获取选中的复选框数,可以使用
count()

注意:当前,只有第一个元素具有
属性。您必须将其添加到其余复选框中,才能使其正常工作


希望这有帮助

谢谢你的快速回复!所以使用那个if语句,我是否需要做一个循环来计算选中了多少复选框?我可以用一个通用命令遍历整个表单,然后将其输入到.csv中吗?我想我现在有点困惑了。看看这里的官方文档:但基本上看一下我的JSFIDLE,它应该做你想做的事情:非常感谢,上面的答案非常有用!谢谢你的快速回复!所以使用那个if语句,我是否需要做一个循环来计算选中了多少复选框?我可以用一个通用命令遍历整个表单,然后将其输入到.csv中吗?我想我现在有点困惑了。看看这里的官方文档:但基本上看一下我的JSFIDLE,它应该做你想做的事情:非常感谢,上面的答案非常有用!这真的很有帮助!我想知道是否有一种方法可以让我不必添加名字,而只使用我所拥有的?@user1292136:当然可以。这只是建议的做法,但如果你想这样做,当然可以做到。类似于
if(isset($\u POST['checkbox-1']){…}
的东西应该可以工作。太棒了!这会增加标签的价值吗?如果我有“英语、意大利语和日语”选中后它会打印英语、意大利语、日语吗?@user1292136:是的,它将获得
属性。在上面的HTML中,只有
英语
具有值属性。您必须将其添加到其余元素中,并且它将按照您的描述工作。啊!有没有办法从您挑剔的抱歉那里获得它ally很有帮助。这真的很有帮助!我想知道是否有一种方法可以让我不必添加名称,而只使用我所拥有的?@user1292136:当然可以。这只是推荐的做法,但如果你想这样做,当然可以。类似于
if(isset($\u POST['checkbox-1']){…}
应该有用。太棒了!这会增加标签的价值吗?如果我有“英语、意大利语和日语”选中后它会打印英语、意大利语、日语吗?@user1292136:是的,它将获得
属性。在上面的HTML中,只有
英语
具有值属性。您必须将其添加到其余元素中,并且它将按照您的描述工作。啊!有没有办法从您挑剔的抱歉那里获得它非常有帮助。
  foreach($_POST['checkbox'] as $value)
  {
     echo 'checked: '.$value.'';
  }