我有一个 jQuery,可以按颜色过滤查询中显示的一些项目。
问题出在“蓝海军蓝”等颜色上。显然 jQuery 没有传递这两个词或其他东西。当我回显 _GET
值的结果时,当传递了 2 个单词颜色时,我永远不会得到任何结果,而我却得到了单单词颜色的值。
知道为什么会发生这种情况吗?
这是我的代码:
jQuery(document).ready(function($) {
$("input:checkbox").change(function() {
if($(this).is(':checked'))
{
$(".loadingItems").fadeIn(300); //fade in on change
var color = $(this).val();
$(".indexMain").load('indexMain.php?color='+color,function(){
$(".indexMain").fadeIn(slow);
})
$(".loadingItems").fadeOut(300); //remove when load is complete
}
else
{
$(".loadingItems").fadeIn(300); //fade in on change
$(".indexMain").load('indexMain.php');
$(".loadingItems").fadeOut(300); //remove when load is complete
}
});
});
$color = $_GET['color'];
$items = $con -> prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items -> bindValue(":colorbase1", $color);
最佳答案
由于它是 GET 请求,因此该值会附加到查询字符串中,并且查询字符串不能包含常规空格。
在查询字符串中使用该值之前,您需要对其进行 url 编码:
$(function() {
$("input[type='checkbox']").on('change', function() {
if (this.checked) {
$(".loadingItems").fadeIn(300);
var color = encodeURI(this.value);
$(".indexMain").load('indexMain.php?color=' + color, function() {
$(".indexMain").fadeIn(slow);
$(".loadingItems").fadeOut(300);
});
} else {
$(".loadingItems").fadeIn(300);
$(".indexMain").load('indexMain.php', function() {
$(".loadingItems").fadeOut(300);
});
}
});
});
$color = $_GET['color'];
$items = $con - > prepare("SELECT * FROM item_descr WHERE color_base1 = :colorbase1");
$items - > bindValue(":colorbase1", $color);
$_GET
超全局变量应该在 PHP 脚本中自动进行 url 解码。
关于PHP jQuery 传递多个单词的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11923888/