如何获取数据并传递。进入表格并回复客户名onkeyup或keydown json和php。
<?php
$conn = new mysqli("localhost", 'root', "", "laravel");
$query = mysqli_query($conn,"select * from customers");
while ($result2=mysqli_fetch_assoc($query)) {
$data[] = $result2['customername'];
}
echo json_encode($data);
?>
以下是使用javascript ajax的HTML代码: -
<div id="demo">
CustomerName: <input type="search" name="customername">
</div>
<script>
var xmlhttp = new XMLHttpRequest();
var url = "connection.php";
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
myFunction(xmlhttp.responseText);
}
}
xmlhttp.open("GET", url, true);
xmlhttp.send();
function myFunction(response) {
var arr = JSON.parse(response);
document.getElementById("demo").innerHTML = arr;
}
</script>
如何在keyup或keydown上传递表单和响应以及相关建议customername应该显示下来。我是JSON和javascript以及示例网站的新手。提前致谢。欢迎提出所有建议。
答案 0 :(得分:0)
来自您的代码示例
$conn = new mysqli("localhost", 'root', "", "laravel");
$query = mysqli_query($conn,"select * from customers");
$dataForJSON = array()
while($result2=mysqli_fetch_assoc($query)) {
$dataForJSON[] = $result2['customerName'];
}
echo json_encode($dataForJSON);
使用此代码从json返回数据,这可以与JSON.parse(response)