I have an html page with a button on it that triggers an ajax request to a php page. So far so good.
I have a database messages which has the columns "id", "receiver", "sender", "message".
Say I have 2 entrys there both the same receiver and sender. The messages are different.
Now upon button click the messages shall be displayed in seperate paragraphs <p>Message 1</p> and <p>Message 2</p>.
My problem is that I don't know how many messages there will be and the button click should display them all. I have tried it with a while loop but can't get it to work properly. Also how would JQuery handle something like this?
My php so far:
$thuser = $_SESSION['username']; //this user
$sql = "SELECT * FROM messages WHERE receiver = '$thuser'";
if ($result = mysqli_query($link, $sql)) {
while ($row = $result->fetch_row()) {
...
});
}
If possible I would like an array as the response containg all messages {'message1' = > 'Hello', 'message2' => 'What is up?', message n => 'blah'}
Thanks for any help!
Jquery?...isn't valid PHP. If you output the HTML in the PHP file, you can simply make a get request in js and take the response and display it directly wherever you want on the page by adding it to the dom (page). Note that most agree outputting JSON from the server (PHP) and processing in JS is the way to do this. You should use your web browsers dev tools to check the get request to see the output it receives. If the output is wrong then it's a PHP issue, if you struggle with getting the output to display on the page using JS then it's a JS issue. You need to figure out which one it is first