简体   繁体   English

发送和处理从jQuery到PHP的关联数组

[英]Sending and processing an associative array from jquery to php

I have a filter for some devices in a webpage, made of checkbox. 我有一个用于复选框的网页中某些设备的过滤器。 Whenever one of the checkbox is clicked, i call a function which add to an object the value of the checkboxes checked. 每当单击复选框中的一个时,我就会调用一个函数,该函数将选中的复选框的值添加到对象中。 I want to send this object to a php file, via ajax, and use it to perform some MySQL query, then return the results from the php and display them on the page. 我想将此对象通过ajax发送到php文件,并使用它执行一些MySQL查询,然后从php返回结果并将其显示在页面上。 The problem is, i'm missing something, since i kept getting a parseerror in my js. 问题是,我丢失了一些东西,因为我一直在我的js中遇到parseerror。

Here's my code: device-filter.js 这是我的代码:device-filter.js

$(document).ready(function(){
$(".ez-checkbox").click(function() {
    console.log("ok");
    var re = {Brand: "", Cost: "", OS: ""};
    $("#Brand :checkbox:checked").each(function(){
        re.Brand += $(this).val()+" & ";
    });
    $("#Cost :checkbox:checked").each(function(){
        re.Cost += $(this).val()+" & ";
    });
    $("#OS :checkbox:checked").each(function(){
        re.OS += $(this).val()+" & ";
    });
    if(re.lenght==0){

    }
    else{
        $.ajax({
            method: "POST",
            dataType: "json", //type of data
            crossDomain: true,
            data: re,
            url:"./php/filtered-device-query.php",
            success: function(response) {
            //display the filtered devices  
            },
            error: function(request,error)
            {
                console.log(request+":"+error);
            }
        });
    }
});
});

filtere-device-query.php filtere-device-query.php

<?php
//connection to db
$mysqli = new mysqli("localhost", "root", "", "my_db");

if (mysqli_connect_errno()) { //verify connection
echo "Error to connect to DBMS: ".mysqli_connect_error(); //notify error
exit(); //do nothing else 
}
else {
//echo "Successful connection"; // connection ok
    $devices =json_decode($_POST['re']);
    echo var_dump($devices)."<br>"; 
    $myArray = array();//create an array
    $brand = rtrim($devices["Brand"], " &");
    $cost = rtrim($devices["Cost"], " &");
    $os = rtrim($devices["OS"], " &");

    $query = " SELECT * FROM `devices` WHERE `Brand` = '$brand' AND 'Cost' = '$cost' AND 'OS' = '$os' ";
    $result = $mysqli->query($query);
    //if there are data available
    if($result->num_rows >0)
    {
        while($row = $result->fetch_array(MYSQL_ASSOC)) {
            $myArray[] = $row;
        }
        echo json_encode($myArray);
    }

    //free result
    $result->close();

    //close connection
    $mysqli->close();
}
?>

Thanks in advance for any help! 在此先感谢您的帮助!

You have some typos, first in the jQuery: 您有一些错字,首先是jQuery:

if(re.lenght==0){

should be: 应该:

if(re.length==0){// note the correct spelling of length

Then in your PHP you're using quotes on column names in the query. 然后在您的PHP中,您在查询中的列名上使用引号。 Those should be removed or better yet, back ticked: 那些应该被删除或更好,回勾:

$query = " SELECT * FROM `devices` WHERE `Brand` = '$brand' AND `Cost` = '$cost' AND `OS` = '$os' ";

More importantly... 更重要的是...

An object, as you've described it, has no length. 正如您所描述的,对象没有长度。 It will come back as undefined . 它将返回为undefined In order to find the length you have to count the keys: 为了找到长度,您必须对键进行计数:

if(Object.keys(re).length == 0){...

The object re , as you've declared it, already has 3 keys, a length of 3. Checking for length of 0 is a waste of time. 正如您所声明的,对象re已经具有3个键,长度为3。检查长度为0浪费时间。


Little Bobby says your script is at risk for SQL Injection Attacks. 小鲍比说, 您的脚本有遭受SQL注入攻击的危险。 Learn about prepared statements for MySQLi . 了解有关MySQLi的 准备好的语句。 Even escaping the string is not safe! 即使转义字符串也不安全!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM