Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从PHP使用AJAX从MYSQL获取数据_Javascript_Php_Mysql_Ajax_Fetch - Fatal编程技术网

Javascript 如何从PHP使用AJAX从MYSQL获取数据

Javascript 如何从PHP使用AJAX从MYSQL获取数据,javascript,php,mysql,ajax,fetch,Javascript,Php,Mysql,Ajax,Fetch,我想使用ajax从数据库调用数组数据。但我不知道怎么走。在ajax调用中将其显示为文本字段时,我感到困惑。如何从我的sql查询中获取数组数据 这是index.php的样子 <body> <div class="container" > <h2>View data</h2> <h4>Word List : </h4> <div class="form-group"

我想使用ajax从数据库调用数组数据。但我不知道怎么走。在ajax调用中将其显示为文本字段时,我感到困惑。如何从我的sql查询中获取数组数据

这是index.php的样子

<body>
<div class="container" >
<h2>View data</h2>
<h4>Word List : </h4>

        <div class="form-group">
        <input id="wordlist" type="text" class="form-control" name="wordlist">
        </div><br>
        <button id="display" title="Generate Word">Generate</button>
        <div class="input-single">
        </div>

        <script type="text/javascript">
        $(document).ready(function() {
        $("#display").click(function() {
        $.ajax({    
            type: "POST",
            url: "getword.php",
            dataType: "json",
            cache: false,
            success: function(result){
              $('#wordlist').html(result[0]);
            }        
        }); 
  });
});

</script>
</body>

查看数据
词表:

生成 $(文档).ready(函数(){ $(“#显示”)。单击(函数(){ $.ajax({ 类型:“POST”, url:“getword.php”, 数据类型:“json”, cache:false, 成功:功能(结果){ $('#wordlist').html(结果[0]); } }); }); });
然后这是urlgetword.php的样子

<?php
$host = "localhost";
$user = "root";
$password = ""; 
$dbname = "wordlist";

$con = mysqli_connect($host, $user, $password, $dbname);
if (!$con) {
 die("Connection failed: " . mysqli_connect_error());
}

$sql = "select kata from word"; 

 $res = mysqli_query($con,$sql); 
 $result = array(); 
 while($row = mysqli_fetch_array($res)){
    $result[]= $row[0];
 }
 echo json_encode(array('kata'=>$result));  
 mysqli_close($con);

?>

id=“wordlist”
是一个输入,您从API传回的是一个对象

{
 kata: [...]
}
所以
$('#wordlist').html(结果[0])
不起作用,您需要使用
$('#单词列表').val(result.kata[0])(注意:不是html),否则它将加入word1、word2、word3等值


如果您的目的不是将所有单词放在一个输入中,那么您应该更改它,使其在
result.kata
上循环,并为每个单词创建一个输入etc

为了澄清,您是否需要从外部调用PHP文件?难道你不能在PHP文件中构建HTML,然后一次性将其发送给服务器吗?或者你必须遵守一些限制吗?我已经按照你的建议通过控制台日志检查了它。然后数据出现在控制台上。那么如何显示到输入字段中?我尝试了您的代码,但在输入字段中,数据变成了Object对象,而没有显示来自$('#wordlist').val(结果[0])的真实数据
I would like to suggest you to debug your code by yourself using following steps :

1.You can check what response you are getting from server side by printing your ajax response data at console to print this use below code.

 success: function(result){
   console.log(result); 
   // $('#wordlist').html(result[0]);
 }
This will print that you are receiving data from server side.
2.Another point is you are handling the response inside an input field if want to print fetch data inside input field then you have to replace $('#wordlist').html(result[0]); from $('#wordlist').val(result[0]); this line from your JavaScript code because it is an input field.