2017-07-30 116 views
0

下面显示的是我的connection.php文件。jQuery datatable不能在php中工作

Pic

我在控制台

遇到这些错误我怎样才能摆脱这些错误的?

 <?php 
     $con=mysqli_connect("localhost", "root", ""); 
     mysqli_select_db($con,"college"); 
     if($con) 
     { 
      //echo "Database Connected"; 
      //echo "<br>"; 
     } 
     else 
     { 
      //echo "Database Not Connected"; 
     } 
     ?> 

这是我的fetch.php文件。我打电话给我所有的CSS和JS文件,但我不知道为什么我的数据表无法正常工作。

 <html> 
     <head> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" > 
     <link rel="stylesheet" href="datatable/media/css/dataTables.jquery.dataTables.css" /> 
     <link rel="stylesheet" href="datatable/media/css/dataTables.bootstrap.css" /> 
     <script src="datatable/media/js/dataTables.jquery.dataTables.js"></script> 
     <script src="datatable/media/js/dataTables.bootstrap.js"></script> 
     </head> 
     <body> 



     <?php 
     include("conn.php"); 
     $sql="select * from student"; 
     $res=mysqli_query($con,$sql); 
     ?> 

     <table border="1" id="datatable"> 
     <thead> 
     <tr> 
     <td>USER NAME</td> 
     <td>FIRST NAME</td> 
     <td>LAST NAME</td> 
     <td>EMAIL ADDRESS</td> 
     <td>PASSWORD</td> 
     </tr> 
     </thead> 
     <tbody> 
     <?php 

     while ($result=mysqli_fetch_array($res)) 
     { 
      ?> 
      <tr> 
      <td><?php echo $result['Student_Uname']; ?></td> 
      <td><?php echo $result['Student_Fname']; ?></td> 
      <td><?php echo $result['Student_Lname']; ?></td> 
      <td><?php echo $result['Email_Id']; ?></td> 
      <td><?php echo $result['Password']; ?></td> 
      </tr> 

     <?php } ?> 
     </tbody> 
     </table> 
     </body> 
     </html> 

     <script> 
     $(document).ready(function() { 
     $('#datatable').DataTable(); 
     }); 
     </script> 

这是我的index.php文件。这是我的学生信息页面。

 <!DOCTYPE html> 
     <html lang="en"> 
     <head> 
     <title>Form Page</title> 
     <meta charset="utf-8"> 
     <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" > 
     <link rel="stylesheet" href="style/css/bootstrap.css" /> 
     <link rel="stylesheet" href="style/css/bootstrap-theme.css" /> 
     </head> 
     <body> 
     <div class="container"> 
     <h2 align="center">STUDENT INFORMATION FORM</h2> 
     <form method="POST" action="insert.php" class="form-horizontal" autocomplete="off"> 
     <div class="form-group"> 
     <label for="Uname" class="control-label col-md-2">User Name:</label> 
     <div class="col-md-8"> 
     <input type="text" name="Uname" class="form-control" placeholder="User Name" required > 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="Fname" class="control-label col-md-2">First Name:</label> 
     <div class="col-md-8"> 
     <input type="text" name="Fname" class="form-control" placeholder="First Name" required > 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="Uname" class="control-label col-md-2">Last Name:</label> 
     <div class="col-md-8"> 
     <input type="text" name="Lname" class="form-control" placeholder="Last Name" required > 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="email" class="control-label col-md-2">Email Address:</label> 
     <div class="col-md-8"> 
     <input type="email" name="email" class="form-control" placeholder="Email Address" required > 
     </div> 
     </div> 
     <div class="form-group"> 
     <label for="password" class="control-label col-md-2">Password:</label> 
     <div class="col-md-8"> 
     <input type="password" name="password" class="form-control" placeholder="Password" autocomplete="new-password" required > 
     </div> 
     </div> 
     <div class="form-group"> 
     <div class="col-sm-offset-2"> 
     <button type="submit" name="submit" class="btn btn-info">Submit</button> 
     </div> 
     </div> 
     </form> 
     </div> 
     <script src="style/js/jquery.js"></script> 
     <script src="style/js/bootstrap.js"></script> 
     </body> 
     </html> 

这是我的insert.php文件,我已正确插入数据库。

 <?php[enter image description here][1] 
     include("conn.php"); 
     if (isset($_POST['submit'])) 
     { 
     $uname=$_POST['Uname']; 
     $fname=$_POST['Fname']; 
     $lname=$_POST['Lname']; 
     $email=$_POST['email']; 
     $pass=$_POST['password']; 

     $q="insert into student(Student_Uname,Student_Fname,Student_Lname,Email_Id,Password) values ('".$uname."', '".$fname."', '".$lname."', '".$email."', '".$pass."')"; 

     $res=mysqli_query($con,$q); 
     } 
     ?> 
     <h3 align="center">YOU ARE MOVING VIEW PAGE</h3> 
     <?php 
     header("refresh:1; url=fetch.php"); 
     ?> 

     [1]: https://i.stack.imgur.com/k8JdX.jpg 

这是我抓取的数据表输出页面,所以通过这张图片你可以了解更多。我满足了jQuery数据表所需的所有要求。错误在哪里?

+0

检查css和js文件是否在浏览器中直接使用而不是从php页面加载 –

回答

0

错误指出您的Javascript和CSS文件的路径不正确。

当使用src="datatable/media/js/dataTables.bootstrap.js",它是说,有一个在SAME目录为您的PHP文件存放在名为datatable子目录。

如果您的网站的根目录中的“datatable”目录需要使用前导正斜杠:src="/datatable/media/js/dataTables.bootstrap.js"。主要的正斜杠表示路径是相对于我的网站的根。

+0

您还必须在数据表库之前加载jQuery库。你也错过了这个参考 –