2017-09-27 14 views
-2

のindex.php私はデータベースとの接続を作成し、AJAXを通じて

まず、PHPでデータベースからデータを取得し、私はAJAXを介してデータを取得するための変数$actionを作成し、<td><tr>てテーブルを設計します。私は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

私は、データベースからデータをフェッチします。私は$.ajaxdataurl、、success関数を呼び出した後、show_all()関数を使用します。私は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); 
      } 
     }); 
    }); 
}); 
+0

あなたはどのようにAJAX作品を見て回ることを試みたことがありますか? –

+5

あなたの質問のタイトルをゴーグルしようとしましたか? –

+0

こんにちは。あなたのajaxコードは大丈夫です。二重引用符で囲み、試してみてください。データ:{"action": "showroom"}、 –

答えて

0
**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({ 

    }); 
}); 
+0

これを私のレベルでやったところ、AJAXを通してデータベースからデータを取り出し、それをデータテーブルにバインドすることができます。 –

0
$.ajax({ 
     data :{"action": "showroom"} , 
     url :"index.php", 
     type :'POST', 
     success: function(data){ 
      $("#content").html(data); 
     } 
     }); 
    } 
+0

私はあなたの提案を実装しましたが、以下のエラーが発生しました 未定義のインデックス:C:\ xampp \ htdocs \ CRUD \ CRUD \ index.php 31行目でこのエラーに直面しています - –

+0

私のデータはAJAXしかし、あなたは何かを説明するために私を助けることができますか? "data:{action:" showroom "}"と$ action = $ _ POST ["action"]のような意味を持ちます。 –

+0

はいdata要素には、URLに投稿しているすべての値が含まれています。ここでのアクションは、名前と同じ形式のキーであり、ショールームはアクションキーの値です。 –

関連する問題