0

Here is my button

$( "#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!");
        }
        });

  });

Here is my 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();
?>

And here is my 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>
        ";
    }

There is an input field in my html where i input the vehicle plate then when the user clicks the button the program searches the database for the vehicle name with the plate the user entered and returns the value to another inputfield named "vehicle_name". Any idea on where am i going wrong here?

1 Answer 1

1

You should rely on JSON to encode and pass information when making AJAX calls — you encode the information in JSON format in the PHP file you called, send it back to your script and allow it to parse:

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']);
    }

However, I usually prefer to echo an array, so that you can pass way more information than just the queried data, e.g. success/error status, and other fields:

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"
            ));
        }

    }

Warning: You should not use mysql_ functions — they are insecure. Use mysqli_ or prepared statements.

After that, you can parse the JSON data with jQuery as a usual object. Remember to declare the dataType property as JSON (even though $.ajax() will attempt to guess intelligently), which is good practice :)

$("#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);
        }
    });

});

And if you prefer to use arrays in your JSON data:

    // 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);
            }
        }
    });
Sign up to request clarification or add additional context in comments.

2 Comments

can you tell where the "vehicleName" from "data.vehicleName" is from?
@user3578118 It is the array key in the PHP. You can of course change it. "vehicleName" => $row['vehicle_name']

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.