如何通过AJAX以正确的格式向PHP发送一组复选框值?

时间:2022-05-14 13:41:04

I have a set of checkboxes, that contain bits of SQL Code in their value. If they're checked, they should be send via ajax to my php file and be embedded in my query.

我有一组复选框,其值包含SQL代码。如果他们被检查,他们应该通过ajax发送到我的PHP文件并嵌入我的查询。

I'm only experienced with simple single variable ajax transfer and i just can't make the examples I find online via google or on * to work.

我只有简单的单变量ajax传输经验,我只是不能通过谷歌或*在网上找到的例子。

My ajax call:

我的ajax电话:

$("#button1").click(function(e) {
    var category_id = {};
    category_id['checkboxvalue'] = $('.checkboxes:checked').serialize();
    $.ajax({
        type: "POST",
        url: "allgemein.php?id=" + Math.random(),
        dataType: "html",
        data: category_id,
        success: function(response) {
            $("#resulttable").show().html(response);
        }
    });
});

My checkbox form:

我的复选框表格:

<input type="checkbox" class="checkboxes" name="checkb[]" value="MASTER.SYSTEM LIKE '%systemA'">System A</label>
<input type="checkbox" class="checkboxes" name="checkb[]" value="(MASTER.SYSTEM LIKE '%systemB' OR MASTER.SYSTEM LIKE '%systemC')">System B/C</label>
<input type="checkbox" class="checkboxes" name="checkb[]" value="MASTER.SERVICE LIKE '%serviceX%'">Service X</label>

Now in my php I want to use the array like this:

现在在我的PHP中我想使用这样的数组:

$strWhere = '1=1';

if(true === isset($_POST['checkboxvalue']) && 0 < sizeof($_POST['checkboxvalue']))
    {
        $strWhere .= 'AND (';
        foreach($_POST['checkboxvalue'] as $checkboxval)
        {
            $strWhere .= '"%'. $checkboxval .'%" OR ';
        }
        //remove last OR
        $strWhere = substring($strWhere, 0, -3);
        $strWhere .= ')';
    }

Then add $strWhere to my SQL Where clause.

然后将$ strWhere添加到我的SQL Where子句中。

1 个解决方案

#1


0  

.serialize() gives you a string of form encoded data.

.serialize()为您提供一个表单编码数据的字符串。

Passing an object to data: will cause jQuery to encode the values in the form as form encoded data.

将对象传递给数据:将导致jQuery将表单中的值编码为表单编码数据。

Since you have already encoded that data, you don't want to reencode it.

由于您已经编码了该数据,因此您不想对其进行重新编码。

Pass the string directly.

直接传递字符串。

data: $('.checkboxes:checked').serialize(),

What can I do, if I have to send a variable via data: because I have other values in it?

如果我必须通过数据发送变量,我该怎么办?因为我有其他值吗?

If you pass data an object then it gets passed through jQuery.param. You can do that manually:

如果您传递数据对象,那么它将通过jQuery.param传递。您可以手动执行此操作:

data: $('.checkboxes:checked').serialize() + "&" + jQuery.param({
  some: "other",
  data: "values"
}),

#1


0  

.serialize() gives you a string of form encoded data.

.serialize()为您提供一个表单编码数据的字符串。

Passing an object to data: will cause jQuery to encode the values in the form as form encoded data.

将对象传递给数据:将导致jQuery将表单中的值编码为表单编码数据。

Since you have already encoded that data, you don't want to reencode it.

由于您已经编码了该数据,因此您不想对其进行重新编码。

Pass the string directly.

直接传递字符串。

data: $('.checkboxes:checked').serialize(),

What can I do, if I have to send a variable via data: because I have other values in it?

如果我必须通过数据发送变量,我该怎么办?因为我有其他值吗?

If you pass data an object then it gets passed through jQuery.param. You can do that manually:

如果您传递数据对象,那么它将通过jQuery.param传递。您可以手动执行此操作:

data: $('.checkboxes:checked').serialize() + "&" + jQuery.param({
  some: "other",
  data: "values"
}),