2016-03-27 18 views
-1

jquery ajaxを使用してデータベースからデータを取得しようとしていました。これはコードです:jQuery ajaxデータベースからのデータ要求が機能しない

<script> 
$(document).ready(function(){ 
function fetch_data(){ 
$.ajax({ 
type:"POST", 
url:"http://localhost:88/phpPoint/select.php", 
success:function(response){$("#livedata").html(response);} 
}); 
} 
fetch_data(); 
/*$(document).on("click","#btnadd",function(){ 
    var firstname=$("#firstname").text(); 
    var lastname=$("#lastname").text(); 
    if(firstname==''){ 
     alert("enter first name"); 
     return false; 
    } 
    if(lastname==''){ 
     alert("enter last name"); 
     return false; 
    } 
    $.ajax({ 
     type:"post", 
     url:"insert.php", 
     data:{firstname:firstname,lastname:lastname}, 
     dataType:"text", 
     success:function(data){alert(data); 
     fetch_data();} 

    }); 
});*/ 
}); 
</script> 

しかし、私はデータを得ませんでした。空のページが表示されます。データをフェッチ PHPのコードは次のとおりです。「mydbrun」私のデータベース内の私の「tblsample」テーブルが2 entries.iはで私の出力を表示したい持っているときには、どのようなデータが表示されない理由を私は知らない

<?php 
$connect=mysqli_connect("localhost","root","***********","mydbrun"); 
$output=$row=""; 
$sql="SELECT * FROM tblsample ORDER BY id DESC"; 
$result=mysqli_query($connect,$sql); 
$output.="<div class='table-responsive'> 
     <table class='table table-bordered'> 
     <tr> 
     <th style='width:10%'>Id</th> 
     <th style='width:40%'>Firstname</th> 
     <th style='width:40%'>Lastname</th> 
     <th style='width:10%'>Delete</th> 
     </tr>"; 
     if(mysqli_num_rows($result)>0){ 
      while($row=mysqli_fetch_array($result)) 
      { 
       $output.="<td>".$row['id']."</td> 
       <td class='firstname' data-id1='".$row['id']."' contenteditable>".$row['firstname']."</td> 
       <td class='lastname' data-id2='".$row['id']."' contenteditable>".$row['lastname']."</td> 
       <td><button name='btndelete' id='btndelete' data-id3='".$row['id']."'>x</button></td>"; 
      } 
      $output.="<tr> 
         <td></td> 
        <td id='firstname' contenteditable></td> 
        <td id='lastname' contenteditable></td> 
     <td><button id='btnadd' name='btnadd' class='btn btn-success'>+</button></td></tr>"; } 
     else{ 
     $output.="<tr><td colspan='4'>Data Not Found</td></tr>";  
     } 
$output.="</table> 
</div>"; 
?> 

idが 'livedata'のdiv要素。 私はhtml5属性contenteditableを使用していますが、それは問題を引き起こしていますか?私はどこかからこのコードをコピーして、私は属性 'data-id1'、 'data-id2'の意味を知らない。 plzヘルプ。前もって感謝します :) 。

+0

あなたのURLは何も返されていません。あなたのPHPファイルの最後に 'print_r(json_encode($ output))'を書きましょう。 – santosh

+0

あなたはサーバーからデータを返しません。 'echo $ output'を追加してください –

+0

ありがとうございました。愚かな間違い:p –

答えて

0

あなたのURLはPHPファイル.of終わり..

print_r(json_encode($output)) 

何も返していません。

とあなたのajaxにdataType:'json'を追加してください。

関連する問題