如何从数据库查询中设置html输入字段值

时间:2014-05-03 23:58:26

标签: php jquery html mysql ajax

这是我的按钮

$( "#editVehicle" ).button().click(function( event ) {

    event.preventDefault();
    var vp = $("input[name=vehicle_plate]").val();

    var dataString = 'vehicle_plate='+ vp;

    $.ajax({
        type: "POST",
        url: "editvehicle.php",
        data: dataString,
        success: function(){
        alert("Success!");
        }
        });

  });

这是我的PHP

<?PHP
include("db.classes.php");
$g = new DB();
$g->connection();

        if($_POST)
            {
                $vehiclePlate = $g->clean($_POST["vehicle_plate"],1);

                $g->edit($vehiclePlate);
            }
$g->close();
?>

这是我的db.classes

public function edit($vehiclePlate)
    {
        $sql = "select vehicle_name from vehicles where vehicle_plate='$vehiclePlate'";

        $result = mysql_query($sql);

        $row = mysql_fetch_array($result);

        echo "<script>

        $(\"input[name=vehicle_model]\").val(".$row['vehicle_name'].");

        </script>
        ";
    }

我的html中有一个输入字段,我输入车牌,然后当用户点击按钮时,程序在数据库中搜索用户输入的车牌的车辆名称,并将值返回到另一个名为&#34的输入字段; vehicle_name&#34 ;.我知道我在哪里错了吗?

1 个答案:

答案 0 :(得分:1)

在进行AJAX调用时,您应该依赖JSON来编码和传递信息 - 您在所调用的PHP文件中以JSON格式编码信息,将其发送回脚本并允许其解析:

public function edit($vehiclePlate)
    {
        $sql = "select vehicle_name from vehicles where vehicle_plate='$vehiclePlate'";
        $result = mysql_query($sql);
        $row = mysql_fetch_array($result);
        echo json_encode($row['vehicle_name']);
    }

但是,我通常更喜欢回显数组,以便您可以传递的信息不仅仅是查询数据,例如成功/错误状态以及其他字段:

public function edit($vehiclePlate)
    {
        $sql = "select vehicle_name from vehicles where vehicle_plate='$vehiclePlate'";
        $result = mysql_query($sql) or die(json_encode(array("error" => 0, "errorMsg" => "MySQL query failed.")));
        $row = mysql_fetch_array($result);
        if(mysql_num_rows($row)) {
            // 1 or more rows are returned
            echo json_encode(array(
                "success" => 1,
                "vehicleName" => $row['vehicle_name'],
            ));
        } else {
            // No rows returned
            echo json_encode(array(
                "error" => 1,
                "errorMsg" => "No rows returned"
            ));
        }

    }

警告:您不应使用mysql_函数 - 它们不安全。使用mysqli_或准备好的陈述

之后,您可以使用jQuery解析JSON数据作为常用对象。请记住将dataType属性声明为JSON(即使$.ajax()会尝试智能猜测),这是一种很好的做法:)

$("#editVehicle").button().click(function(e) {

    // Prevent default action
    e.preventDefault();

    // You can declare all variables in this scope with on var statement
    var vp = $("input[name=vehicle_plate]").val(),
        dataString = 'vehicle_plate='+ vp;

    // Make magic happen
    $.ajax({
        type: "POST",
        url: "editvehicle.php",
        data: dataString,
        dataType: "json",    // Declare dataType
        success: function(data){
            $("input[name=vehicle_model]").val(data);
        }
    });

});

如果您更喜欢在JSON数据中使用数组:

    // Make magic happen
    $.ajax({
        type: "POST",
        url: "editvehicle.php",
        data: dataString,
        dataType: "json",    // Declare dataType
        success: function(data){
            if(!data.error && data.success) {
                $("input[name=vehicle_model]").val(data.vehicleName);
            } else {
                alert(data.errorMsg);
            }
        }
    });