Ajax does not populate the table

I am trying to populate my table using the so-called modern method of updating parts of a site asynchronously using Ajax. So far I have managed to populate the table using pure php. Here is the code. Thank you in advance for your hints of a solution to this problem!

function showUsersBasic(){
$.ajax({
        url: 'php_dbrequests\php_requests.php',
        type:'POST',
        dataType: 'json',
        success: function(output_string){
                $('#db-table-users').append(output_string);
            } // End of success function of ajax form
        }); // End of ajax call    }

Here is the code in php_dbrequests \ php_requests.php

<?php
require 'database.php';
$sql = "SELECT id, email, regdate FROM users";
$records = $conn->prepare( $sql );
$records->execute();
$results = $records->fetchAll( PDO::FETCH_ASSOC );
    $output_string = '';
    $output_string .=  '<table>';
    $output_string .= '<tr>';
    $output_string .= '<th>ID</th>';
    $output_string .= '<th>Email</th>';
    $output_string .= '<th>Register Date</th>';
    $output_string .= '</tr>';
foreach( $results as $row ){
    $output_string .=  "<tr><td>";
    $output_string .=  $row['id'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['email'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['regdate'];
    $output_string .=  "</td><td>";
    $output_string .=  "</td>";
    $output_string .=  "</tr>";
}
    $output_string .= '</table>';
echo json_encode($output_string);?>

I know I got the most basic information: my db connection is successful, the button calling the ajax function works. I double checked my link to the table. Please any hints would be greatly appreciated!

+4
source share
5 answers

PHP-, , data, table, table html. tr .

<?php
require 'database.php';
$sql = "SELECT id, email, regdate FROM users";
$records = $conn->prepare( $sql );
$records->execute();
$results = $records->fetchAll( PDO::FETCH_ASSOC );
    $output_string = '';
    #$output_string .=  '<table>';
    $output_string .= '<tr>';
    $output_string .= '<th>ID</th>';
    $output_string .= '<th>Email</th>';
    $output_string .= '<th>Register Date</th>';
    $output_string .= '</tr>';
foreach( $results as $row ){
    $output_string .=  "<tr><td>";
    $output_string .=  $row['id'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['email'];
    $output_string .=  "</td><td>";
    $output_string .=  $row['regdate'];
    $output_string .=  "</td><td>";
    $output_string .=  "</td>";
    $output_string .=  "</tr>";
}
    #$output_string .= '</table>';
echo json_encode(array('data'=>$output_string) );?>

ajax :

function showUsersBasic(){

            console.info('showUserBasic called');  

  $.ajax({
    url: 'php_dbrequests\php_requests.php',
    type:'POST',
    dataType: 'json',
    success: function(output_string){
            //OPEN FIREFOX FIREBUG EXTENSION & check console and click the ajax request to see its response
            console.log(output_string);           

            $('#db-table-users').append(output_string.data);
        } // End of success function of ajax form
    }); // End of ajax call   
 }
+1

, $.get(), PHP ajax.

function showUsersBasic() {
    $.get("php_dbrequests\php_requests.php", function(output_string, status){
        $('#db-table-users').append(output_string);
    });
 }
0

? ? ( id)

dev, , ajax - - alert(response); .

* Doh! - . , php script .

0

:

success: function(response){
                $('#db-table-users').append(response.output_string);
            } // End of success function of ajax form
        }); // End of ajax call    }
0

I believe you should remove the "dataType" parameter from your ajax call code. You expect JSON, but it returns html. You also need to remove json_encode()from echo in php.

0
source

Source: https://habr.com/ru/post/1662362/


All Articles