【发布时间】:2016-06-05 07:53:17
【问题描述】:
我有一组复选框,它们的值包含一些 SQL 代码。如果它们被选中,它们应该通过 ajax 发送到我的 php 文件并嵌入到我的查询中。
我只对简单的单变量 ajax 传输有经验,我只是无法使我通过 google 或 stackoverflow 在线找到的示例正常工作。
我的 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);
}
});
});
我的复选框表单:
<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>
现在在我的 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 .= ')';
}
然后将 $strWhere 添加到我的 SQL Where 子句中。
【问题讨论】:
-
data: category_id:category_id -
@Drudge 你是什么意思?通常,当我不发送数组而只是发送单数时,多个值
data:category_id,工作正常..
标签: php jquery mysql arrays ajax