0

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!

4
  • this is quite broad. What have you tried using Jquery? Commented Nov 25, 2017 at 14:23
  • @D.Krold You should create a php file to handle request in ajax from jquery. That PHP will return the list of all messages in JSON Array. you will receive that in front end on success of ajax. Use that array to run loop and print the p in page using jquery. If still you need more help feel free to comment. Commented Nov 25, 2017 at 14:24
  • ... 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 Commented Nov 25, 2017 at 14:24
  • I generally now how to get a json response and handle that if I have a set amount of variables, then it's quite easy. My trouble is that I don't know how to create an array without previously knowing how many entries it will have. Commented Nov 25, 2017 at 14:30

1 Answer 1

1

Use Array in your case :

$message=array();

$thuser = $_SESSION['username']; //this user
$sql = "SELECT * FROM messages WHERE receiver = '$thuser'";

if ($result = mysqli_query($link, $sql)) {
 $i=1;
  while ($row = $result->fetch_row()) {
   $st='Message'.$i;
  $message[$st]=$row['message'];
   $i++;
  });

}
print_r($message);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.