我第一次使用JQuery。尝试使用自动填充功能,并继续收到上述错误。 我测试了MySQL查询,它们都有效。 如果我使用下面相同的代码但是从没有自动填充代码的网页调用, JSON看起来很好。 感谢您提供的任何指导。
<?php
$docname=$_POST['docselect'];
$surname=$_POST['lastname1'];
if (isset($_POST['lastname1'])){
$return_arr = array();
try {
$dbc=mysqli_connect('localhost','gotlibc_testuser','**passwordgoeshere**','gotlibc_robo2') or die("Error connecting to MySQL server.");
$query="SELECT PT_ID, surname, firstname, jnum FROM patients WHERE surname LIKE '$surname%' ORDER BY surname";
$result=mysqli_query($dbc,$query) or die('Error querying database.');
$row=array();
while($Xrow = mysqli_fetch_array($result)) {
$row['label']="{$Xrow['surname']},{$Xrow['firstname']},{$Xrow['jnum']}";
$row['value']=$Xrow['PT_ID'];
$return_arr[]=$row;
}
}
// end try
catch(Exception $e ) {
echo "<script type='text/javascript'> alert('Hi'); </script>";
printf("catch activated");
echo $e->errorMessage();
}
echo json_encode($return_arr);
mysqli_close($dbc);
}
?>
以下是调用网页的相关摘要
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
$(document).ready(function(){
$( "#lastname1" ).autocomplete({
source: "pt_autoshow1.php", minLength: 1
});
});
</script>
<h2>Patient Information:</h2>
<br>
<form id="surnamepicker" action="#" method="post"> or Last name: <input name="lastname1" id="lastname1" type="text">
<div class="rage_button_781076"> <a href="">New Patient</a> </div>
with Dr.
<select name="docname">
<option value="2">Doctor A</option>
<option value="1">Doctor B</option>
<option value="3">Doctor C</option>
</select></form>
答案 0 :(得分:1)
它对我来说很好 php函数将返回行/无结果作为响应
public function shamsearchJSON ()
{
$search = $this->input->post('search');
$query = $this->user_model->getmessages($search);
if(!empty($query)){
echo json_encode(array('responses'=> $query));
}
else{
echo json_encode(array('responses'=> 'No Results'));
}
}
javascript代码
$( "#search-inbox" ).autocomplete({
minLength: 2,
source: function( request, response ) {
// $.getJSON( "<?php echo base_url(); ?>index.php/user/shamsearchJSON?search="+request.term,response );
$.ajax({
url: "<?php echo base_url(); ?>index.php/user/shamsearchJSON",
data: {"search": request.term},
type:"POST",
success: function( data ) {
var parsed = JSON.parse(data);
if(parsed.responses == "No Results")
{
alert("no results::");
var newArray = new Array(1);
var newObject = {
sub: "No Results",
id: 0
};
}
else{
var newArray = new Array(parsed.length);
var i = 0;
parsed.responses.forEach(function (entry) {
var newObject = {
sub: entry.subject,
id: entry.mID
};
newArray[i] = newObject;
i++;
});
}
response(newArray);
},
error:function(){
alert("Please try again later");
}
});
},
focus: function( event, ui ) {
//$( "#search-inbox" ).val( ui.item.sub );
return false;
},
select: function( event, ui ) {
if(ui.item.id != 0){
$( "#search-inbox" ).val( ui.item.sub );
openInboxMessage(ui.item.id);
}
else
{
}
return false;
}
}).data( "ui-autocomplete" )._renderItem = function( ul, item ){
return $( "<li>" ).append("<a>" + item.sub +"</a>" ).appendTo(ul);
};