获得使用PHP和jQuery的AJAX从MySQL数据库中的数据 [英] Get data from mysql database using php and jquery ajax

查看:287
本文介绍了获得使用PHP和jQuery的AJAX从MySQL数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP和jQuery的AJAX从MySQL数据库中的数据。 process.php'是连接到数据库并获取MySQL数据的PHP文件。它的工作原理,当它被单独运行,但在使用AJAX称为这是行不通的。是否有人可以帮助纠正错误?这里是我的html文件:

 < HTML>
< HEAD>
<脚本类型=文/ JavaScript的SRC =JS / jQuery的-1.11.0.min.js>< / SCRIPT>
<脚本类型=文/ JavaScript的>
$(文件)。就绪(函数(){
    功能陈列室(){
        $阿贾克斯({
            键入:POST,
            网址:process.php
            数据:{行动:陈列室},
            成功:功能(数据){
                $(#内容)HTML(数据);
            }
        });
    }
    陈列室();
});
< / SCRIPT>
< /头>
<身体GT;
< D​​IV ID =内容>< / DIV>
< /身体GT;
< / HTML>
 

下面是我的process.php文件

 < PHP
$链接= mysqli_connect(本地主机,根,红莓,homebot);

如果(mysqli_connect_errno())
    回声无法连接到MySQL:。 mysqli_connect_error();

$行动= $ _ POST [行动];
如果($行动==展示厅){
    $查询=SELECT * FROM用户;
    $显示= mysqli_query($链接,$查询)或死亡(错误);
    而($行= mysqli_fetch_array($展)){
        回声<李> $行['名称']< /李>中;
    }
}
?>
 

解决方案

有你的AJAX呼叫的两个语法错误:

  $(文件)。就绪(函数(){
    功能陈列室(){
        $阿贾克斯({
            键入:POST,
            网址:process.php
            数据:{行动:陈列室},
            成功:功能(数据){
                $(#内容)HTML(数据);
            }
        });
    }
    陈列室();
});
 

请记住,jQuery的阿贾克斯希望一个对象作为参数。在对象内部的语法是

  {键:值}
 

您定义的对象键时有TYPE =POST这是在声明语法正确,但不正确的。

其次,上述目的的数据属性应该太一个对象。而不是行动=展厅所以它应该是

  {行动:陈列室}
 

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:

<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

<?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>";
    }
}
?>

解决方案

There are two syntax errors in your ajax call:

$(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

{ key : value }

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

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

{action:"showroom"}

这篇关于获得使用PHP和jQuery的AJAX从MySQL数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆