0
I’m doing a chat (chat) using PHP and Mysql, and I’m having problems.
I think it has to do with the fact that php 5.5
not accept more mysql, (now you need to use mysqli) Well, I have two files:
index php.
<html>
<head>
<title>Chat Box</title>
<script>
function submitChat() {
if(form1.uname.value == '' || form1.msg.value == '') {
alert('ALL FIELDS ARE MANDATORY!!!');
return;
}
var uname = form1.uname.value;
var msg = form1.msg.value;
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState==4&&xmlhttp.status==200) {
document.getElementById('chatlogs').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET','insert.php?uname='+uname+'&msg='+msg,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form name="form1">
Enter Your Chatname: <input type="text" name="uname"><br>
Your Message: <br>
<textarea name="msg"></textarea><br>
<a href="#" onclick="submitChat()">Send</a><br><br>
<div id="chatlogs">
LOADING CHATLOGS PLEASE WAIT...
</div>
</body>
</html>
Insert.php
<?
$uname = $_REQUEST['uname'];
$msg = $_REQUEST['msg'];
$con = mysqli_connect('XXX','XXX','XXX','XXX');
mysqli_query($con,"INSERT INTO logs ('username','msg') VALUES ('$uname','$msg')");
$result1 = mysqli_query($con,"SELECT * FROM logs ORDER by id DESC");
while($extract = mysqli_fetch_array($result1)) {
echo "<span class='uname'>" . $extract['username'] . "</span>: <span class='msg'>" . $extract['msg'] . "</span><br>";
}
?>
I have a domain where the code is: mundozoeira.com.br
So you can go there to see the behavior.
What’s the matter? @Renan
– Wellington Avelino
The chat just doesn’t work. I send the message and nothing happens. I mean, the phrase "LOADING CHATLOGS PLEASE WAIT..." disappears, it is replaced using innerHTML. I have no idea what it is...
– user27503
Could be an error in mysql database?
– user27503
Brother, after your connection put this code.
if ( $con ->connect_errno )
 {
 printf("Erro na Conexão: %s\n", $con ->connect_error);
 exit();
 }
– MoisesGama
I already did. Nothing happened.
– user27503