我可以在自动完成后将json打印到两个输入中。问题是他们显示了所有的json。例如:
[{"cliName":"GRC Att: S. Gosselin","proDescription":"Entretien GRC"}]
但我只想保留名称:GRC Att: S. Gosselin
作为第一个输入,第二个我需要保留:entretien GRC
这是我的autocomplete.php代码:
<!-- numéro de projet du dimanche -->
<td>
<span id="numpro" >
<form method="post" action="">
<input type="text" id="name" name="name"onkeypress="return handleEnter(event, this, 'task');"/>
</form>
</span>
</td>
</script>
<!-- client du dimanche -->
<td>
<span id="proclient">
<input type="text" name="client1" size="12" disabled id ="client1" class "client" >
</span>
</td>
<!-- description du projet de dimanche -->
<td>
<span id="prodesc">
<input type="text" name="desc1" size="30" disabled id ="desc1" class "desc">
</span>
</td>
这是我的index.php
的代码<script type="text/javascript">
$(document).ready(function(){
$("#name").autocomplete({
source:'getautocomplete.php',
minLength:1
});
});
function handleEnter(e, obj, field){
if (e.keyCode == 13 || e.which == 13){
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var json = xmlhttp.responseText;
alert(json);
//document.getElementById("desc1").value=window.atob(xmlhttp.responseText);
//document.getElementById("client1").value=window.atob(xmlhttp.responseText1);
$("#client1").val(json);
$("#desc1").val(json);
}
}
xmlhttp.open("GET","completeclient.php?q="+obj.value,true);
xmlhttp.send();
}
//Enter was pressed, handle it here
}
</script>
最后我在哪里获取我的信息completeclient.php
<?php
//connect to db
$mysqli = new mysqli($host_name,$user_name,$pass_word, $database_name);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
//mysqli_select_db($database_name, $con);
$var="";
$var=$_GET["q"];
$stmt = $mysqli->prepare("select cliName,proDescription from projets,clients where proNum like '%$var%' AND proCliID = cliID ");
//$json=array();
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
$json[]=array(
'cliName'=> $row['cliName'],
'proDescription'=>$row['proDescription']
);
//$message = ($row['cliName']);
// echo (base64_encode($message));
echo json_encode($json);
//echo $result;
//$datas = array('client' => $row['cliName'], 'description' => $row['proDescription']);
//json_encode($datas);
?>
感谢您的帮助!