I am trying to create a simple chat application. Below, you notice that my PHP code is inside a div, which is for the chat data, and that div is inside another div representing the chat box. Inside my PHP code, I wrote my PHP function. It connects to the myPHPadmin server and then it has a query to place the inputted data from the person's name into the database. I have an echo statement to write what the person wrote. As soon as I press the submit button, it will show what the person wrote, but if I type another message in, the message text gets replaced. How do I get my PHP code to dynamically create echo statements as if it were a chat conversation? It should create a new line each time I send a message.
<div id="chat_box">
<div id="chat_data">
<?php
function sendMessage()
{
//I hid my login credentials
$servername = "";
$username = "";
$password = "";
$dbname = "";
// Create connection
$dbc = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($dbc->connect_error)
{
die("Connection failed: " . $dbc->connect_error);
}
$name = $_POST['name'];
$msg = $_POST['log'];
$query = "INSERT INTO `chatApp` (`name`, `pwd`, `message`) VALUES ('$name', NULL, '$msg')";
$run = $dbc->query($query);
echo "<p>" . $name . " : </p> ";
echo "<p>" . $msg . "</p>";
}
?>
</div>
</div>