virat121
virat121

Reputation: 95

How to take database values as objects in ajax to display in html table?

I have a html button, when I click it will go to the jQuery ajax code and it will execute result.php an get success message to display values in same page.

Here is my index page

<button id="data" name="data">get database result</button>

<table id="table1"> 
    //Let jQuery AJAX Change This Text  
</table>

jquery code in same page

    $(document).ready(function(){

  $('#data').click(function(event){

      event.preventDefault();

       $.ajax({

          url   : "result.php",
          dataType  : "text",
          success : function(suc){

             $('#table1').text(suc)

          }

       });


  });

});

And my result.php file

    <?php 

 require_once 'class.user.php';

 $getUser = new USER();

  $query = "SELECT name , email FROM user";

  $stmt = $getUser->runQuery($query);

   if( $stmt->execute()){

     echo "<table border='1' >
            <tr>
            <td align=center> <b>Name</b></td>
            <td align=center><b>Email</b></td>";

     while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {

        echo "<tr>";
        echo "<td align=center>" .$row['name'] ."</td>";
        echo "<td align=center>" . $row['email']. "</td>";
        echo "</tr>";

     }

   }
   else{

    echo "fail";
   }



?>

when i click on button, All database values oare display in table format in same index page..

i used "text" as dataType in ajax parameter..

what i have to do..

output

enter image description here

Any suggestions will be greatly thankfull...

Upvotes: 0

Views: 45

Answers (1)

ahbou
ahbou

Reputation: 4928

Since the response is in HTML, your success function should be:

 $('#table1').html(suc)

Also you already have the table tag in your html so no need for this line in php:

 <table border='1' >

Upvotes: 2

Related Questions