Witam.
Próbuję nauczyć się jquery (obecnie znikoma styczność) i ajaxa (brak wiedzy na ten temat). Uznałem, że najlepiej rozpocząć naukę od "przepisania" czegoś sensownego np. czat-u i spróbowania zrozumienia co tam autor dobrego robi. Wszystko szło ładnie i było zrozumiałe aż nie zaczął się ajax (w 4 odcinku). Kod który przepisywałem nie działa (nie zapisuje tekstu do bazy danych) i teraz nie wiem czy się gdzieś pomyliłem czy... no właśnie, bo na filmiku wszystko działa. Nie wiem co jest grane bo nie wyrzuca mi błędów. Mogę prosić o rzut okiem na kod? Znając mojego farta brakuje jakiegoś przecinka lub mam nie tą bibliotekę ;(
link do poradnika (chodzi mi o ostatnie minuty gdzie tekst jest wprowadzony do bazy danych): https://www.youtube.com/watch?v=ssOPDgBMoiE
cały kod:
index.php
<!DOCTYPE HTML>
<html lang="pl">
<head>
<meta charset="utf-8" />
<title>Chat</title>
<script type="text/javascript" src="js/jquery.js"></script>
<link rel="stylesheet" href="Style/Style.css" type="text/css" />
</head>
<body>
<h2>LOGIN FORM</h2>
<div id="LoginDiv">
<form method="post" action="pages/UserLogin.php">
<table>
<tr>
<td>Email: </td><td><input type="email" name="UserMailLogin" /></td>
</tr>
<tr>
<td>Password: </td><td><input type="password" name="UserPasswordLogin" /></td>
</tr>
<tr>
<td></td><td><input type="submit" value="LOG IN" /></td>
</tr>
<?php
if (isset($_GET['error'])) {
?>
<tr>
<td></td><td><span style="color:red">Error Login</span></td>
</tr>
<?php
}
?>
</table>
</form>
</div>
<br /><br /><br />
<h2>SING UP FORM</h2>
<div id="SingUpDiv">
<form method="post" action="pages/InsertUser.php">
<table>
<tr>
<td>Your Name: </td><td><input type="text" name="UserName" /></td>
</tr>
<tr>
<td>Your Email: </td><td><input type="mail" name="UserMail" /></td>
</tr>
<tr>
<td>Password: </td><td><input type="password" name="UserPassword" /></td>
</tr>
<tr>
<td></td><td><input type="submit" value="SING UP" /></td>
</tr>
<?php
if (isset($_GET['success'])) {
?>
<tr>
<td></td><td><span style="color:green">User Inserted</span></td>
</tr>
<?php
}
?>
</table>
</form>
</div>
</body>
</html>
Home.php
<?php
session_start();
?>
<!DOCTYPE HTML>
<html lang="pl">
<head>
<meta charset="utf-8" />
<title>Chat</title>
<link rel="stylesheet" href="../Style/Style.css" type="text/css" />
<script type="text/javascript" src="../js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#ChatText").keyup(function(e) {
if (e.keyCode ==13) {
var ChatText = $("#ChatText").val();
$.ajax({
type:'POST',
url:'InsertMessage.php',
data:{ChatText:ChatText},
success:function() {
$("#ChatText").val("");
}
});
}
});
});
</script>
</head>
<body>
<h2>Welcome <span style="color:green"><?php echo $_SESSION['UserName']; ?></span></h2>
<br/><br/>
<div id="ChatBig">
<div id="ChatMessages">
</div>
<textarea id="ChatText" name="ChatText"></textarea>
</div>
</body>
</html>
conn.php
<?php
try {
$bdd = new PDO("mysql:host=localhost;dbname=chat","root","");
} catch(Exception $e) {
die("ERROR: ".$e->getMessage());
}
?>
classes.php
<?php
class user {
private $UserName, $UserMail, $UserPassword;
public function getUserId() {
return $this->UserId;
}
public function setUserId($UserId) {
$this->UserId=$UserId;
}
public function getUserName() {
return $this->UserName;
}
public function setUserName($UserName) {
$this->UserName=$UserName;
}
public function getUserMail() {
return $this->UserMail;
}
public function setUserMail($UserMail) {
$this->UserMail=$UserMail;
}
public function getUserPassword() {
return $this->UserPassword;
}
public function setUserPassword($UserPassword) {
$this->UserPassword=$UserPassword;
}
public function InsertUser() {
include "conn.php";
$req=$bdd->prepare("INSERT INTO users(UserName,UserMail,UserPassword) VALUE(:UserName,:UserMail,:UserPassword)");
$req->execute(array(
'UserName'=>$this->getUserName(),
'UserMail'=>$this->getUserMail(),
'UserPassword'=>$this->getUserPassword()
));
}
public function UserLogin() {
include "conn.php";
$req=$bdd->prepare("SELECT * FROM users WHERE UserMail=:UserMail AND UserPassword=:UserPassword");
$req->execute(array(
'UserMail'=>$this->getUserMail(),
'UserPassword'=>$this->getUserPassword()
));
if($req->rowCount()==0) {
header("Location: ../index.php?error=1");
return false;
} else {
while($data=$req->fetch()) {
$this->setUserId($data['UserId']);
$this->setUserName($data['UserName']);
$this->setUserMail($data['UserMail']);
$this->setUserPassword($data['UserPassword']);
header("Location: Home.php");
return true;
}
}
}
}
class chat {
private $ChatId, $ChatUserId, $ChatText;
public function getChatId() {
return $this->ChatId;
}
public function setChatId($ChatId) {
$this->ChatId=$ChatId;
}
public function getChatUserId() {
return $this->ChatUserId;
}
public function setChatUserId($ChatUserId) {
$this->ChatUserId=$ChatUserId;
}
public function getChatText() {
return $this->ChatText;
}
public function setChatText($ChatText) {
$this->ChatText=$ChatText;
}
public function InsertChatMessage() {
include "conn.php";
$req=$bdd->prepare("INSERT INTO chats(ChatUserId,ChatText) VALUE(:ChatUserId,:ChatText)");
$req->execute(array(
'ChatUserId'=>$this->getChatUserId(),
'ChatText'=>$this->getChatText()
));
}
}
?>
InsertUser.php
<?php
include "classes.php";
$user = new user();
if (isset($_POST['UserName']) && isset($_POST['UserMail']) && isset($_POST['UserPassword'])) {
$user->setUserName($_POST['UserName']);
$user->setUserMail($_POST['UserMail']);
$user->setUserPassword($_POST['UserPassword']);
$user->InsertUser();
header("Location: ../index.php?success=1");
}
?>
UsertLogin.php
<?php
session_start();
include "classes.php";
$user = new user();
$user->setUserMail($_POST['UserMailLogin']);
$user->setUserPassword($_POST['UserPasswordLogin']);
if ($user->UserLogin()==true){
$_SESSION['UserId']=$user->getUserId();
$_SESSION['UserName']=$user->getUserName();
$_SESSION['UserMail']=$user->getUserMail();
}
?>
InsertMessage.php
<?php
session_start();
include "classes.php";
if (isset($_POST['ChatText'])) {
$chat = new chat();
$chat->setChatUserId($_SESSION['UserId']);
$chat->setChatText(($_POST['ChatText']);
$chat->InsertChatMessage();
}
?>
jquery.js
http://code.jquery.com/jquery-2.1.0.min.js
ps. nie podałem tu styli.css i bazy z phpmyadmin bo nie chcę robić jeszcze większego spamu niż jest ;)
ps2. dziękuję wszystkim za poświęcony czas na przeczytanie tego posta i pomoc