Jump to content

Returning json_encode as array not working


fanboime

Recommended Posts

Here is my button

 

    

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

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

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

});


 

And 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, driver_id 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))
{
echo json_encode(array(
"success" => 1,
"vehicleName" => $row['vehicle_name'],
"assignedDriver" => $row['driver_id']
));
}
else
{
echo json_encode(array(
"error" => 1,
"errorMsg" => "No rows returned"
));
}

}


 

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 and driver_id with the plate the user entered and returns the value to another inputfield named "vehicle_name" and "assigned_driver" but nothing is happening when the button is clicked not even the error alert message. Any idea on where am i going wrong here?

Link to comment
Share on other sites

Try this

dataString = {'vehicle_plate' : vp};

First thing to do otherwise is do a check on the receiving php page to see what POST vars are being sent through and do a print_r() of that.  Assuming your ajax call is getting to the correct page the print_r() shoudl display in the console of html depending on where you ave that going.

Link to comment
Share on other sites

nothing no errors

Nothing? So the request is not being made? Have you looked in the network tab? The request / response? What does the response look like?

 

First thing to do otherwise is do a check on the receiving php page to see what POST vars are being sent through and do a print_r() of that. Assuming your ajax call is getting to the correct page the print_r() shoudl display in the console of html depending on where you ave that going.

print_r() really? Do you not have a php debugger?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.