Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.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 逗号分隔复选框$\u使用jquery获取查询请求_Php_Jquery - Fatal编程技术网

Php 逗号分隔复选框$\u使用jquery获取查询请求

Php 逗号分隔复选框$\u使用jquery获取查询请求,php,jquery,Php,Jquery,尝试使用多个复选框创建搜索函数,但似乎只查找与$\u POST请求相关的信息 <input type="checkbox" name="fruit" value="1"> <input type="checkbox" name="fruit" value="2"> <input type="checkbox" name="fruit" value="3"> <input type="checkbox" name="fruit" value="4">

尝试使用多个复选框创建搜索函数,但似乎只查找与$\u POST请求相关的信息

<input type="checkbox" name="fruit" value="1">
<input type="checkbox" name="fruit" value="2">
<input type="checkbox" name="fruit" value="3">
<input type="checkbox" name="fruit" value="4">
<input type="checkbox" name="fruit" value="5">
有没有一种非ajax的方法可以做到这一点

只是想澄清一下

每个值代表不同的结果

最初我是这样做的

当我添加更多复选框时,查询似乎效率低下

我知道我可以使用$\u POST方法将复选框添加到数组中

<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">

因此,问题实际上是如何清理它(逗号分隔)

注意:正如其他人所指出的,没有必要通过一个带有逗号分隔值的单个参数在服务器上以数组或逗号分隔值结束。像PHP这样的框架可以在不需要JavaScript的情况下处理这个问题。您可以简单地为每个复选框赋予相同的“name”属性。这将导致多个同名参数被传递到服务器,这很好。事实上,如果使用
元素,您将得到相同的结果

在PHP中,如果使用末尾带有方括号的名称,如
fruit[]
,则可以在服务器上获得一个包含以下内容的数组:

$_GET['fruit']
如果要在服务器上使用逗号分隔的值,可以使用:

implode(',', $_GET['fruit'])
但是,如果您真的想要一个带有逗号分隔值的单个参数,下面是您可以使用的方法:

您可以使用带有隐藏输入的表单。将表单的“方法”设置为
“获取”
,将隐藏输入的“名称”设置为
“水果”
。然后添加一个事件处理程序,在提交表单时将隐藏输入的值设置为逗号分隔的字符串

HTML:

注意:复选框元素没有“name”属性,因此它们不会包含在表单提交中


注意:正如其他人所指出的那样,在服务器上不必传递带有逗号分隔值的单个参数,而最终得到数组或逗号分隔值。像PHP这样的框架可以在不需要JavaScript的情况下处理这个问题。您可以简单地为每个复选框赋予相同的“name”属性。这将导致多个同名参数被传递到服务器,这很好。事实上,如果使用
元素,您将得到相同的结果

在PHP中,如果使用末尾带有方括号的名称,如
fruit[]
,则可以在服务器上获得一个包含以下内容的数组:

$_GET['fruit']
如果要在服务器上使用逗号分隔的值,可以使用:

implode(',', $_GET['fruit'])
但是,如果您真的想要一个带有逗号分隔值的单个参数,下面是您可以使用的方法:

您可以使用带有隐藏输入的表单。将表单的“方法”设置为
“获取”
,将隐藏输入的“名称”设置为
“水果”
。然后添加一个事件处理程序,在提交表单时将隐藏输入的值设置为逗号分隔的字符串

HTML:

注意:复选框元素没有“name”属性,因此它们不会包含在表单提交中


使用
水果[]
作为输入名称

<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">

使用
水果[]
作为输入名称

<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">

尝试此操作,使用
复选框
名称作为
水果[]
。在php中,使用
内爆()获取水果数组并转换为字符串


试试这个,使用
复选框
名称作为
水果[]
。在php中,使用
内爆()获取水果数组并转换为字符串




您需要将表单的方法设置为
GET
复选框需要不同的名称;单选按钮共享名称属性。。。。或者它们需要用
[]
命名,其中PHP将以数组形式接收它们
name=“fruit[]”
,但这将使查询字符串
search?fruit[]=1&fruit[]=2&fruit[]=5
如果要使其成为逗号分隔的列表,则需要编写脚本。为什么要用逗号分隔?没有JavaScript就无法实现这一点(但你不需要通过AJAX实现)。使用隐藏输入重建它们?你需要将表单的方法设置为
GET
复选框需要不同的名称;单选按钮共享名称属性。。。。或者它们需要用
[]
命名,其中PHP将以数组形式接收它们
name=“fruit[]”
,但这将使查询字符串
search?fruit[]=1&fruit[]=2&fruit[]=5
如果要使其成为逗号分隔的列表,则需要编写脚本。为什么要用逗号分隔?如果没有JavaScript(但你不需要通过AJAX)就无法做到这一点。使用隐藏输入重构它们?你可以使用
fruit[]
作为输入名称,PHP(以及其他一些框架)将自动准确地处理这一点,这正是我想要的。谢谢John@naomik-这似乎没有必要,但它符合OP的要求。我认为,至少从问题(编辑后)可以清楚地看出,OP知道其他选择。@JohnS,这很公平。如果您编辑答案以确认这一点,我可以删除我的否决票^。^@naomik-我已经在我的答案中添加了一条注释。您只需使用
fruit[]
作为输入名称,PHP(以及其他一些框架)将自动正确地处理我所寻找的问题。谢谢John@naomik-这似乎没有必要,但它符合OP的要求。我认为,至少从问题(编辑后)可以清楚地看出,OP知道其他选择。@JohnS,这很公平。如果您编辑答案以确认这一点,我可以删除我的否决票^。^@naomik-我已经在我的答案中添加了注释。
$('#fruitForm').submit(function() {
    var fruits = $('#fruitForm').find('input:checkbox:checked').map(function() {
        return $(this).val();
    }).get().join(',');

    $('#fruitForm').find('input[name=fruit]').val(fruits);
});
<input type="checkbox" name="fruit[]" value="1">
<input type="checkbox" name="fruit[]" value="2">
<input type="checkbox" name="fruit[]" value="3">
<input type="checkbox" name="fruit[]" value="4">
<input type="checkbox" name="fruit[]" value="5">
echo "<pre>", print_r($_GET["fruit"], true);
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
)
    <?php
       echo $fruits = implode(",",$_GET['fruit']); //1,2,3,4,5
    ?>
    <form>
    <input type="checkbox" name="fruit[]" value="1">
    <input type="checkbox" name="fruit[]" value="2">
    <input type="checkbox" name="fruit[]" value="3">
    <input type="checkbox" name="fruit[]" value="4">
    <input type="checkbox" name="fruit[]" value="5">        
    <input type="submit">
   </form>