1

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'];
};
8
  • This is a 5th grader question but any ways the flow would be to "Send the data to the server(PHP) via Ajax, PHP gets the required Data via MySQL query and process it further(If Required) and send the response back." It is simple enough but you have to explore more. Commented Nov 23, 2015 at 20:18
  • you need to echo $row[0]['timein'] Commented Nov 23, 2015 at 20:21
  • 1
    @Osama So how does my code fail to accomplish that? Your response wasn't very helpful. Commented Nov 23, 2015 at 20:21
  • Did you try to print_r($_POST)? What do you get when you do that because i believe you are not even getting anything at server side. Commented Nov 23, 2015 at 20:23
  • 3
    Is the personal attack on his question necessary? Just answer it or don't Commented Nov 23, 2015 at 20:27

1 Answer 1

1

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);
             }
 });
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help Goon3r.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.