Не работает код написанный на js.Ошибка Unexpected token 'function'.
<?php
$b = [];
$inp = file_get_contents("php://input");
if(isset($_POST["nick"]) && $_POST["text"]){
$nick = $_POST["nick"];
$text = $_POST["text"];
$a = "<b>".$nick."</b></br>".$text."<br>";
$fd = fopen("chat.txt","a");
fwrite($fd,"\n");
fwrite($fd,$a);
fclose($fd);
}
if($inp == "ourForm_inp=1"){
$fd = fopen("chat.txt","r");
while(!feof($fd))
{
$str = htmlentities(fgets($fd));
array_push($b,$str);
}
fclose($fd);
for($i = 0;$i <= count($b);$i++){
echo $b[$i];
}
}
?>
<html>
<head>
<meta charset = "utf-8">
<title>Chat</title>
</head>
<body>
<style>
#div{
margin: 0;
padding: 0;
width: 100%;
height: 90%;
background-color: #565755;
text-overflow: scroll;
}
form{
margin: 0;
padding: 0;
width: 100%;
height: 10%;
}
#input{
margin: 0;
padding: 0;
width: 100%;
height: 50%;
}
#input1{
margin: 0;
padding: 0;
width: 80%;
height: 50%;
float: left;
}
#input2{
width: 20%;
height: 50%;
float: left;
}
#input2:hover{
background-color: red;
}
</style>
<div id = "div"></div>
<form method = "POST">
<input id="input" type="text" name="nick" placeholder="Имя" value="<?= $_POST['nick'] ?? '' ?>" required />
<input id = "input1" required type = "text" name = "text" placeholder = "Текст">
<input id = "input2" type = "submit" name = "submit" value = "Отправить">
</form>
<script>
var div = document.getElementById('div'),
function send(){
var xhr = new XMLHttpRequest();
text = encodeURIComponent(text);
xhr.open("POST","index.php");
xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xhr.send("ourForm_inp=" + "1");
xhr.onreadystatechange = function(){
if(xhr.readyState == 4 && xhr.status== 200){
div.push(xhr.responseText());
}
}
}
setInterval(send,1000);
</script>
</body>
</html>