I am trying to create a autofill form. I have a drop down menu and some form fields like text boxes. When user click on drop down menu options Ajax call initiate and it will check data in Mysql database. if record found then it will display data in relevant text box.
My code is working fine but i am not able to return data from mysql database.
<script type="text/javascript">
$(function() {
$("#autofill").change(function() {
var data1= $('option:selected', this).text();
$.ajax({
type: "GET",
url:"autofill.php",
cache: false,
dataType: 'json',
data: 'action1=' + data1,
beforeSend: function() {
$("#validation-errors").hide().empty();
},
success: function(data) {
if(data.success == false)
{
var arr = data.errors;
$.each(arr, function(index, value)
{
if (value.length != 0)
{
$("#validation-errors").append('<div class="alert alert-danger"><strong>'+ value +'</strong><div>');
}
});
$("#validation-errors").show();
$('html, body').animate({scrollTop: $("#validation-errors").offset().top}, 2000);
btn.button('reset');
} else {
data = JSON.parse( data );
$('#title').val('data.title');
$('#total').val('data.total');
$('html, body').animate({scrollTop: $("#features-left-image").offset().top}, 2000);
}
},
error: function(xhr, textStatus, thrownError) {
alert('Something went to wrong.Please Try again later...');
btn.button('reset');
alert(thrownError);
}
});
return false;
});
});
</script>
autofill.php
<?php
require_once('include/db.php');
if(isset($_GET['action1'])){
$search = strip_tags(trim($_GET['action1']));
$search = strtolower($search);
$search="%".$search."%";
$result = $mysqli->prepare("SELECT * FROM posting WHERE title LIKE ?");
$result ->bind_param("s", $search);
$result->execute();
$result->store_result();
//$result->bind_result($title);
//$data[]=$result;
echo json_encode($data);
}
?>
As per my understanding problem is around.
//$result->bind_result($title);
//$data[]=$result;
echo json_encode($data);
Please advise me to fix this issue.
Thanks