简体   繁体   中英

Display content from database on HTML page via Ajax and PHP

I'd like to post some content from a database on an html page. I'm not sure what the best way to do this is but my guess would be something that resembles the code below. Please let me know if there is a better way.

HTML:

$(document).ready(function(){
     $.ajax({
        type : "POST",
        url : 'tablepageload.php',
        data : 'test',
        success: function(data) {
            $('#echobox').html(data);
        }
     });
});

PHP:

if (isset($_POST['test'])) {
    $sendtable = "SELECT `timein` FROM `timestamp` WHERE id='" . $latestrow . "' LIMIT 1"; 
    $result = mysqli_query($link, $sendtable);
    $row = mysqli_fetch_array($result);

    echo $row['timein'];
};

Your workflow seems fine to me.

In essence, as already mentioned the work flow for what you're trying to achieve is:

  • Make request (your initial ajax call)
  • Process the request send response (your php script)
  • Handle the reponse (your 'success' callback)

Looking at your code i have some pointers.

Considering using the jquery .load() function. If your ajax call is to do nothing more than populate a div you may aswell use this.

In terms of your sql query, i would recommend looking at:

Hope this proves helpful

EDIT: Also noticed a problem with your ajax call:

$.ajax({
    type : "POST",
    url : 'tablepageload.php',
    //data : 'test',
    //$_POST['test'] = "some_value",
    //$_POST['another'] = "test"
    data : {test:'some_value', another:'test'}, 
    success: function(data) {
             $('#echobox').html(data);
             }
 });

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