eyalix eyalix - 5 months ago 15
Javascript Question

Sort output from MySql with php

I try to return table values from Mysql db sorted
. I manage to connect and update the table right,
at the same page I wish to return the table back to html as chat history. just can't get the messages to show up sorted by time .
2) how is there a better way to return the output with ajax & jquery to the last div id messages?

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$sender = $_POST["sender"];

$receiver = $_POST["receiver"];

$message = $_POST["message"];

date_default_timezone_set('Asia/Tel_Aviv');

$create_date = date('Y-m-d H:i:s');

//Not sure this is right way to update the date.
if ($create_date != $update_date)
$date == $update_date;

$sql = "INSERT INTO messages (sender, receiver, message, create_date) VALUES ('$sender', '$receiver', '$message', '$create_date')";


if (mysqli_query($conn, $sql)) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}


// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT id, sender, receiver, message, create_date FROM messages";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo $row["id"]. ") " . $row["sender"]. " to " . $row["receiver"]. " : " . $row["message"]. " / " . $row["create_date"]."<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>


Html

<form action="" method="POST" enctype="multipart/form-data">
<p>name of sender:
<input class="input" name="sender" id="sender" value="" size="13"
maxlength="13" dir="ltr" autocomplete="on" type="text" height="20" required><br>
<p>name of receiver:
<input class="input" name="receiver" id="receiver" value="" size="13"
maxlength="13" dir="ltr" autocomplete="on" type="text" height="20" required><br>
<p>Message: <br>
<textarea name="message" id="message" value="" rows="5" cols="30" dir="ltr" required></textarea><br>
<input value="Submit" name="button" alt="submit" onsubmit="return checkForm(this);" border="0" type="submit" align="absmiddle"></p>

</form>

<P>MESSAGES</P>
<div id="messages">

<?php include_once("action.php") ?>


Javascript validation
(can't work )
I used required in html5 instead



function checkForm(form)
{
if($('#sender').val() == ''){
alert("Sender name can not be left blank");
form.sender.focus();
return false;
}
if($('#receiver').val() == ''){
alert("Receiver name can not be left blank");
form.receiver.focus();
return false;
}
if($('#message').val() == ''){
alert("Message input can not be left blank");
form.message.focus();
return false;
}
return true;
}


printscreen

Answer

Maybe you can change your select query to

SELECT id, sender, receiver, message, create_date FROM messages ORDER BY create_date