Dealing with returned JSON data from PHP - AJAX JQUERY -
i've been trying retrieve json data php. have html table opens modal. i'm passing id of data in row via ajax post, within php file plan relevant row in database using passed id , return information dispaly in modal. ive been trying return id along temp values.
javascript:
var id = recipient; console.log("id: " + id); var idobject = { id: id } $.ajax({ url: "json.php", type: "post", data: idobject, datatype: "json", contenttype: "application/json" })
json.php:
<?php $id = $_post["id"]; $firstname = "firstname"; $lastname = "lastname"; $email = "email@hotmail.co.uk"; if(isset($id)){ $data = array( "id" => $id, "firstname" => $firstname, "lastname" => $lastname, "email" => $email ); echo json_encode($data); } ?>
i tried using success call function not had luck. please point me in right direction?
try this:
$.ajax({ url: "json.php", type: "post", data: {id: id}, datatype: "json", contenttype: "application/json" }).success(function(data){ //data json object containing data //you outputted php, example: $('#form-email').val(data.email); });
also consider using http status codes or returning status
field in json when there's no $id
set, have consistent response.
example (using php 5.4+
short array syntax):
if(isset($id)) { return json_encode([ 'status' => 'ok', 'errors' => [], //(rest of data) ]); } else { return json_encode([ 'status' => 'error', 'errors' => ['no id given'], ]); }
Comments
Post a Comment