I found some old code which I am trying to understand and unfortunately I did not leave comments which leads to my difficulties. Even with the jQuery documentation, I can't wrap my head around it; I really have looked, but I still don't understand. This program is basically a chat where you type your name and your message and click submit. Then it displays your message in a chat box similar to Facebook. Here is the working code:
My JS
$(document).ready(function() {
$('#submit').on('click', function() {
var name = $('#name').val();
var shout = $('#shout').val();
var outText = 'name=' + name + '&shout=' + shout;
if (name == '' || shout == '') {
alert("Please fill in your name and shout");
} else {
$.ajax({
type: "POST",
url: "database.php",
data: outText,
cache: false,
success: function(html) {
$('#shouts ul').prepend(html);
}
});
}
return false;
});
});
and my PHP
<?php
$connection = mysqli_connect('localhost','root','','muntasirshoutbox');
if(isset($_POST['name']) && isset($_POST['shout']))
{
$name = mysqli_real_escape_string($connection,$_POST['name']);
$shout = mysqli_real_escape_string($connection,$_POST['shout']);
$query = "INSERT INTO shouts(name,shout) ";
$query.= "VALUES ('$name','$shout')";
$result = mysqli_query($connection,$query);
if(!$result)
{
echo "doesent work".mysqli_error($connection);
}
else
{
echo '<li>'.$name.': '.$shout.'</li>';
}
}
?>
So I am confused as to what outText is actuallly doing, the reason I am confused is because, I was under the impression that my PHP was printing the text inside the chat box when I do this echo '<li>'.$name.': '.$shout.'</li>';
, but after experimenting with outText
, I noticed that if I change some stuff with outText
the message I type in wont appear at all in the chat box, or it will appear with whatever string I added to it. What exactly is this doing?!