PHPコードで、「ラベル」の「値」を変更してみてください。
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$row_array['label'] = $row[$field];
array_push($return_arr,$row_array);
}
echo json_encode($return_arr);
理由はわかりませんが、これは重要なようです。私のために働いた例を添付します。私の場合、PHPファイルを介してjQueryからMySQL接続を行う必要がありました。ユーザー名を入力できるオートコンプリートフィールドを作成したかったのですが、ユーザー名をクリックすると、関連するフィールド(名前、名前、メールアドレスなど)が入力されました。これが私のコードです:
HTMLコード:
<html lang="en">
<head>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
</script>
<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>
<!--Here there is my jQuery script-->
<script type="text/javascript" src="scripts/myjQuery.js"></script>
<title>EXAMPLE AUTOCOMPLETE</title>
</head>
<body>
<h1>CREATING A NEW CASE</h1>
<form id='newcase' action="#" method="POST" name="newcase">
<label>Add Nickname </label>
<input class="input-250" type="text" id="nickname" name="nickname"><br/>
<label>First Name </label>
<input class="input-250" type="text" id="firstname" name="firstname"><br/>
<label>Surname </label>
<input class="input-250" type="text" id="surn" name="surn"><br/>
<label>Organisation</label>
<input class="input-250"type="text" id="organisation" name="organisation"><br/>
<label>E-Mail Address </label>
<input class="input-250" type="text" id="email" name="email"><br/>
</form>
</body>
</html>
myjQuery.js:
$(document).ready(function(){
$('#nickname').autocomplete({
//Here I call the PHP file and the method inside this file, separated by '/'.
//You should manage it somehow to make this possible.
//I have managed it whith a PHP file called index.php, that gets whatever it comes
//from the URL after the 'rt' and it separates it by the slash,
//being the first value the name of the file, and the second value the name of the
//method.
source:'index.php?rt=jscallsController/listNicknameUsers',
minLength:1,
select: function(evt, ui)
{
// when a username is selected, populate related fields in this form
document.getElementById('firstname').value = ui.item.firstname;
document.getElementById('surn').value = ui.item.surname;
document.getElementById('organisation').value = ui.item.org;
document.getElementById('email').value = ui.item.mail;
}
});
});
そして、PHPファイルjscallsController.php:
<?php
class jscallsController{
public function listNicknameUsers(){
$hostdb = 'localhost';
$namedb = 'tests';
$userdb = 'username';
$passdb = 'password';
$term = trim(strip_tags($_GET['term']));
$users = 'table_users';
$data = array();
try {
// Connect and create the PDO object
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
// Sets encoding UTF-8
$conn->exec("SET CHARACTER SET utf8");
//Define and perform the SQL SELECT query
$sql = "SELECT u_name, u_fname, u_surname, u_organisation, u_email FROM $users
WHERE u_name LIKE '$term%'";
$result = $conn->query($sql);
// If the SQL query is succesfully performed ($result not false)
if($result !== false) {
// Number of returned rows
$rows = $result->rowCount();
//If exists, returns 1
if($rows > 0){
foreach($result as $user) {
/*The first position of the array needs to be called 'label',
otherwise it will not show anything in the HTML field
where the autocomplete is done.*/
$data[] = array(
'label' => $user['u_name']." (".$user['u_fname']." ".$user['u_surname'].")" ,
'firstname' =>$user['u_fname'],
'surname' => $user['u_surname'],
'org' => $user['u_organisation'],
'mail' => $user['u_email']
);
}
}
}
//Disconnect
$conn = null;
//We send back the array to the jQuery
echo json_encode($data);
}
catch(PDOException $e) {
echo $e->getMessage();
}
}
}
?>