使用php和jquery ajax从mysql数据库中获取数据

时间:2022-09-25 21:31:59

I want to get data from mysql database using php and jquery ajax. 'process.php' is the php file which connects to database and get mysql data. It works when it is run separately, but when called using ajax it doesn't work. Can someone please help to correct error? Here is my html file:

我想使用php和jquery ajax从mysql数据库中获取数据。 'process.php'是连接数据库并获取mysql数据的php文件。它在单独运行时有效,但是当使用ajax调用时它不起作用。有人可以帮助纠正错误吗?这是我的html文件:

<html>
<head>
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    function showRoom(){
        $.ajax({
            type:"POST",
            url:"process.php",
            data:{action:showroom},
            success:function(data){
                $("#content").html(data);
            }
        });
    }
    showRoom();
});
</script>
</head>
<body>
<div id="content"></div>
</body>
</html>

Here is my process.php file

这是我的process.php文件

<?php
$link=mysqli_connect("localhost","root","raspberry","homebot");

if (mysqli_connect_errno())
    echo "Failed to connect to MySQL: " . mysqli_connect_error();

$action=$_POST["action"];
if($action=="showroom"){
    $query="SELECT * FROM user";
    $show=mysqli_query($link,$query) or die ("Error");
    while($row=mysqli_fetch_array($show)){
        echo "<li>$row['name']</li>";
    }
}
?>

2 个解决方案

#1


4  

There are two syntax errors in your ajax call:

您的ajax调用中有两个语法错误:

$(document).ready(function(){
    function showRoom(){
        $.ajax({
            type:"POST",
            url:"process.php",
            data:{action:"showroom"},
            success:function(data){
                $("#content").html(data);
            }
        });
    }
    showRoom();
});

Keep in mind that jQuery's ajax expects an object as parameter. Inside an object the syntax is

请记住,jQuery的ajax期望一个对象作为参数。在对象内部的语法是

{ key : value }

You had type="POST" which is correct in declarative syntax, but incorrect when defining an object key.

你有type =“POST”,它在声明性语法中是正确的,但在定义对象键时是不正确的。

Second, the data property of the aforementioned object should be an object too. So instead of action=showroom it should be

其次,上述对象的数据属性也应该是一个对象。所以不应该采取行动=陈列室

{action:"showroom"}

#2


1  

you did mistake in your code:

你在代码中犯了错误:

 echo "<li>$row['name']</li>";

This should be:

这应该是:

 echo "<li>".$row['name']."</li>";

try that...

试试......

#1


4  

There are two syntax errors in your ajax call:

您的ajax调用中有两个语法错误:

$(document).ready(function(){
    function showRoom(){
        $.ajax({
            type:"POST",
            url:"process.php",
            data:{action:"showroom"},
            success:function(data){
                $("#content").html(data);
            }
        });
    }
    showRoom();
});

Keep in mind that jQuery's ajax expects an object as parameter. Inside an object the syntax is

请记住,jQuery的ajax期望一个对象作为参数。在对象内部的语法是

{ key : value }

You had type="POST" which is correct in declarative syntax, but incorrect when defining an object key.

你有type =“POST”,它在声明性语法中是正确的,但在定义对象键时是不正确的。

Second, the data property of the aforementioned object should be an object too. So instead of action=showroom it should be

其次,上述对象的数据属性也应该是一个对象。所以不应该采取行动=陈列室

{action:"showroom"}

#2


1  

you did mistake in your code:

你在代码中犯了错误:

 echo "<li>$row['name']</li>";

This should be:

这应该是:

 echo "<li>".$row['name']."</li>";

try that...

试试......