在控制台日志中,我看到使用json_encode()返回以下数据;检查此附加图像。
当我点击发送按钮时,它正在使用
调用 submit.php 页面显示数据<?php
print_r($_REQUEST);
?>
它告诉我
Array ( [data] => Shibbir, )
但是我希望得到cdid
和label
键值,因为您可以看到我的附加图片,它在控制台日志中显示了cdid和标签值。
HTML代码:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Multiple, remote</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<style>
.ui-autocomplete-loading {
background: white url("loading-image.gif") right center no-repeat;
}
</style>
<script>
$(function() {
function split( val ) {
return val.split( /,\s*/ );
}
function extractLast( term ) {
return split( term ).pop();
}
$( "#birds" )
// don't navigate away from the field on tab when selecting an item
.bind( "keydown", function( event ) {
if ( event.keyCode === $.ui.keyCode.TAB &&
$( this ).autocomplete( "instance" ).menu.active ) {
event.preventDefault();
}
})
.autocomplete({
source: function( request, response ) {
$.getJSON( "search.php", {
term: extractLast( request.term )
}, response );
},
search: function() {
// custom minLength
var term = extractLast( this.value );
if ( term.length < 2 ) {
return false;
}
},
focus: function() {
// prevent value inserted on focus
return false;
},
select: function( event, ui ) {
var terms = split( this.value );
// remove the current input
terms.pop();
// add the selected item
terms.push( ui.item.value );
// add placeholder to get the comma-and-space at the end
terms.push( "" );
this.value = terms.join( ", " );
return false;
}
});
});
</script>
</head>
<body>
<form method="post" action="submit.php" name="form">
<div class="ui-widget">
<input type="text" id="birds" name="data" size="50">
<input type="submit" value="send">
</div>
</form>
</body>
</html>
search.php页面
<?php
require_once("../frontend/config.php");
$term = $_GET['term'];
$sql = mysqli_query($link, "SELECT cdid, family_name, given_name FROM contact_details WHERE family_name LIKE '%$term%' ");
$return_arr = array();
while ($row = mysqli_fetch_array($sql, MYSQL_ASSOC)) {
$row_array['cdid'] = $row['cdid'];
$row_array['label'] = $row['family_name'];
array_push($return_arr, $row_array);
}
echo json_encode($return_arr);
?>