kybak
kybak

Reputation: 860

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'];
};

Upvotes: 1

Views: 1236

Answers (1)

JParkinson1991
JParkinson1991

Reputation: 1286

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);
             }
 });

Upvotes: 1

Related Questions