Evening, I am trying to grab information from a PHP script using JQuery when the user enters a value into a text input field. The text input box has an auto suggest function so that when the user begins tying they can see what matches from the database.
If the user selects one of the options, JQuery's .change() function then uses AJAX to fetch the data from the database and return the values as JSON.
How do I display the JSON information in a format so that i can use it throughout the rest of the page, i.e. populate form fields?
Here is my jQuery/AJAX;
$(document).ready(function() {
$('#kinaseEntry').change(function () {
$('#waiting').show(500);
$('#message').hide(0);
alert("We have a change");
$.ajax({
type : 'POST',
url : 'post.php',
datatype: 'json',
data: {
kinaseEntry : $('#kinaseEntry').val()
},
success : function(data) {
//SUCCESS CODE IN HERE
},
error : function(error) {
alert("Oops, there was an error!");
}
});
return false;
});
});
and the php script;
<?php
//Include connection to databse
require_once 'connect.php';
if (isset($_POST['userInput'])) {
$input = mysql_real_escape_string ($_POST["userInput"]);
$findInput = "SELECT * FROM table where item = '" .$input. "' ";
if ($result = mysql_query($findInput)) {
$row = mysql_fetch_array($result);
$item1 = $row['item1'];
$item2 = $row['item2'];
$item3 = $row['item3'];
$item4 = $row['item4'];
$item5 = $row['item5'];
/* JSON ROW */
$json = array ("item1" => $item1, "item2" => $item2, "item3" => $item3, "item4" => $item4, "item5" => $item5, ...etc);
} else {
/* CATCH ANY ERRORS */
$json = array('error' => 'Mysql Query Error');
}
/* SEND AS JSON */
header("Content-Type: application/json", true);
/* RETURN JSON */
echo json_encode($json);
/* STOP SCRIPT */
exit;
}
?>