简体   繁体   中英

jQuery ajax request for data from a database not working

i was trying to get data from my database using jquery ajax . this is the code:

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

but i didnt got the data . it shows a empty page. the php code which fetches the data is :

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

i dont know why its not showing any data when my 'tblsample' table in my database 'mydbrun' has 2 entries.i want to display my output in a div element whose id is 'livedata'. I am using html5 attribute contenteditable , is it causing a problem ? i have copied this code from somewhere and i dont know what the attribute 'data-id1' ,'data-id2' mean . plz help . thanks in advance :) .

your url is not returning anything..

 print_r(json_encode($output)) 

at the end .of your php file.

and add dataType:'json' in your ajax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM