How can I assign results from Ajax call into a variable?
I have a drop down list. when user make a choice. I want ajax to call a PHP page to find related data from server and auto fill that into an input field.
See below code
$(document).on('change','#dropdownchoiceid', function(){
$id = 1; // passing in some variable for php to use
$.ajax({
url: "../folder/getdata.php",
type: "POST",
data: {
'id' : $id
},
success:function(data){
$result = data;
console.log($result);
$('#inputid').val($result.column1);
$('#inputid2').val($result.column2);
}
});
The php is working. I get the result in a object that look like this
array(1) {
[0]=>
object(stdClass)#5 (4) {
["id"]=>
string(1) "2"
["column1"]=>
string(7) "20.0000"
["column2"]=>
string(14) "someinfo"
["column3"]=>
string(1) "1"
}
So I am trying to change the input field value with the datas I got from the server. but I get blanks in the console.log. I dont quite understand the code below. I tried it because I see this answer on stackoverflow for similar questions
success:function(data){
$result = data;
}
the PHP is
<?php
//Create class Object
$mProduct = new mProduct;
$id = $_POST['id'];
$result= $mProduct->getData($id);
the model PHP page is
public function getData($id){
$this->db->query("
SELECT
*
FROM rebate
WHERE id= :id
");
//bind
$this->db->bind(':id', $id);
//Assign Result Set
$result = $this->db->resultset();
return $result;
}