Here my PHP code gets called during a jQuery AJAX call:
<?php include '../code_files/conn.php'; $conn = new Connection(); $query = 'SELECT Address_1, Address_2, City, State, OfficePhone1, OfficePhone2, Fax1, Fax2, Email_1, Email_2 FROM clients WHERE ID = ?'; $conn->mysqli->stmt_init(); $stmt = $conn->mysqli->prepare($query); $stmt->bind_param('s', $_POST['ID']); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); echo json_encode($row); ?>
And client code:
$.post(url, {ID:$('#ddlClients').val()}, function(Result){
AJAX call completed successfully. I get the result value as
"{"Address_1":"Divisional Office 1","Address_2":"The XYZ Road",.....and so on
I want to be able to use the values returned as Result.Address_1, Result.Address_2, etc. But I can not do this using the code above. I tried using $row = $result->fetch_object() and $row = $result->fetch_array() but didn't use it.
And I know that this can be done with this server side code:
$row = $result->fetch_assoc(); $retVal = array("Address_1"=>$row['Address_1'], "Address_2"=>$row['Address_2'].......); echo json_encode($retVal);
or
$row = $result->fetch_object(); $retVal = array("Address_1"=>$row->Address_1, "Address_2"=>$row->Address_2.......); echo json_encode($retVal);
Is there a way to send $row directly to the client side of JavaScript and is ready to use it as a JSON object without manually creating the array first?