【问题标题】:Fetch data from database in php through AJAX,通过 AJAX 从 php 中的数据库中获取数据,
【发布时间】:2017-09-27 09:43:03
【问题描述】:

index.php

首先我创建一个与数据库的连接,我通过<td><tr>设计表,我创建一个变量$action通过AJAX获取数据。我使用mysqli_fetch_array 从数据库中获取数据。

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated

// using mysqli_query instead
?>

<html>
<head>  
    <title>Homepage</title>
    <link rel="stylesheet" href="DataTables/datatables.css" type="text/css">
    <link rel="stylesheet" href="DataTables/DataTables/css/dataTables.bootstrap.css" type="text/css">
    <link rel="stylesheet" href="DataTables/DataTables/css/jquery.dataTables.css" type="text/css">
    <script src="DataTables/datatables.js"></script>

    <script src="style/jquery-3.2.1.js"></script>

    <script src="style/datatable.js"></script>

    <script src="DataTables/DataTables/js/dataTables.bootstrap.js"></script>
    <script src="DataTables/DataTables/js/jquery.dataTables.js"></script>   
</head>

<body>
<a href="add.html">Add New Data</a><br/><br/>
<table id="datatable" class="display" width='100%' border=0>
    <thead>
        <tr bgcolor='#CCCCCC'>
            <td>Name</td>
            <td>Age</td>
            <td>Email</td>
            <td>Update</td>
        </tr>
    </thead>
    <?php 
    //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 

    //$action=$_POST["action"];
    //if($action=='showroom')   
    {
    $result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
    while($res = mysqli_fetch_array($result)) {         
        echo "<tr>";
        echo "<td>".$res['name']."</td>";
        echo "<td>".$res['age']."</td>";
        echo "<td>".$res['email']."</td>";  
        echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";       
    }
    }
    ?>
    </table>
</body>
</html>

Add.html

<html>
<head>
    <title>Add Data</title>
    <script src="style/jquery-3.2.1.js"></script>
    <script src="style/insert.js"></script>
    <script src="style/view.js"></script>
</head>
<body>
    <a href="index.php">Home</a>
    <br/><br/>  
    <table bgcolor="orange" align="center" width="25%" border="0">
        <tr> 
            <td>Name</td>
            <td><input type="text" name="name" id="name"></td>
        </tr>
        <tr> 
            <td>Age</td>
            <td><input type="text" name="age" id="age"></td>
        </tr>
        <tr> 
            <td>Email</td>
            <td><input type="text" name="email" id="email"></td>
        </tr>
        <tr> 
            <td></td>
            <td><input type="submit" name="Submit" id="submit" value="Add"></td>
        </tr>
    </table>    

    <button type="button" id="submitBtn">Show All</button>
    <div id="content"></div>    
</body>
</html>

view.js

我从数据库中获取数据。我使用show_all() 函数,然后调用$.ajaxdataurltypesuccess 函数。我第一次尝试通过 AJAX 从数据库中获取数据。

$(document).ready(function(e) {
    $('#submitBtn').click(function() {

        debugger;

        $.ajax({
            //data :{action: "showroom"},
            url  :"index.php", //php page URL where we post this data to view from database
            type :'POST',
            success: function(data){
                $("#content").html(data);
            }
        });
    }); 
});

【问题讨论】:

  • 您是否尝试过了解 ajax 的工作原理?
  • 你试过看问题的标题吗?
  • 嗨。您的 ajax 代码很好,只需将操作用双引号括起来并尝试。数据 :{"action": "showroom"} ,
  • 你不需要 php 文件上的 &lt;html&gt;,&lt;body&gt;,&lt;head&gt; 标签,你只想要数据,干净。

标签: javascript php jquery html ajax


【解决方案1】:
**index.php**

<?php
//including the database connection file
include_once("config.php");

//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated


 // using mysqli_query instead
?>

<html>
<head>  
    <title>Homepage</title>
    <link rel="stylesheet" href="DataTables/datatables.css" type="text/css">
    <link rel="stylesheet" href="DataTables/DataTables/css/dataTables.bootstrap.css" type="text/css">
    <link rel="stylesheet" href="DataTables/DataTables/css/jquery.dataTables.css" type="text/css">
    <script src="DataTables/datatables.js"></script>

    <script src="style/jquery-3.2.1.js"></script>

    <script src="style/datatable.js"></script>

    <script src="DataTables/DataTables/js/dataTables.bootstrap.js"></script>
    <script src="DataTables/DataTables/js/jquery.dataTables.js"></script>





</head>

<body>
<a href="add.html">Add New Data</a><br/><br/>

    <table id="datatable" class="display" width='100%' border=0>
    <thead>
    <tr bgcolor='#CCCCCC'>
        <td>Name</td>
        <td>Age</td>
        <td>Email</td>
        <td>Update</td>
    </tr>
    </thead>
    <?php 
    //while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array 

    //$action=$_POST["action"];
    //if($action=='showroom')

    {
    $result = mysqli_query($mysqli, "SELECT * FROM users ORDER BY id DESC");
    while($res = mysqli_fetch_array($result)) {         
        echo "<tr>";
        echo "<td>".$res['name']."</td>";
        echo "<td>".$res['age']."</td>";
        echo "<td>".$res['email']."</td>";  
        echo "<td><a href=\"edit.php?id=$res[id]\">Edit</a> | <a href=\"delete.php?id=$res[id]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";       
    }
    }
    ?>
    </table>
</body>
</html>


**add.html**

<html>
<head>
    <title>Add Data</title>
    <script src="style/jquery-3.2.1.js"></script>
    <script src="style/insert.js"></script>
    <script src="style/view.js"></script>

</head>

<body>
    <a href="index.php">Home</a>



    <br/><br/>


        <table bgcolor="orange" align="center" width="25%" border="0">
            <tr> 
                <td>Name</td>
                <td><input type="text" name="name" id="name"></td>
            </tr>
            <tr> 
                <td>Age</td>
                <td><input type="text" name="age" id="age"></td>
            </tr>
            <tr> 
                <td>Email</td>
                <td><input type="text" name="email" id="email"></td>
            </tr>
            <tr> 
                <td></td>
                <td><input type="submit" name="Submit" id="submit" value="Add"></td>
            </tr>
        </table>


        <button type="button" id="submitBtn">Show All</button>
        <div id="content"></div>


</body>
</html>

**view.js**

$(document).ready(function(e) {
    $('#submitBtn').click(function()
    {
        debugger;



        $.ajax({
            //data :{action: "showroom"},
            url  :"index.php", //php page URL where we post this data to view from database
            type :'POST',
            success: function(data){



                $("#content").html(data);

                }

            });



    }); 
});


**datatable.js**


$(document).ready(function() {
    $('#datatable').DataTable( {

    } );
} );

【讨论】:

  • 我在我的关卡中完成了这项工作,现在您可以通过 AJAX 从数据库中获取数据并将其绑定到数据表。
【解决方案2】:
$.ajax({
        data :{"action": "showroom"} ,
        url  :"index.php", 
        type :'POST',
        success: function(data){
           $("#content").html(data);
        }
        });
   }

【讨论】:

  • 先生,我实施了您的建议,但出现以下错误通知:未定义的索引:第 31 行 C:\xampp\htdocs\CRUD\CRUD\index.php 中的操作我面临此错误 –
  • 我做了我的数据,我通过 AJAX 正确插入,但你能帮我解释一下吗?类似于 "data :{action: "showroom"}、" 和 $action=$_POST["action"]; 的含义
  • yes 数据元素包含我们发布到 url 的所有值。这里的action是key,形式和name一样,showroom是action key的值。
猜你喜欢
  • 1970-01-01
  • 2011-09-17
  • 2019-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 1970-01-01
相关资源
最近更新 更多