I'm writing a chat script but I have problem with echo
some variables!
I have written this piece of code in chat.php to show the messages:
<?php
session_start();
$id=$_GET['id'];
if(isset($id)){
global $id;
global $qs;
global $answerer;
global $sp;
global $name;
include('config.php');
$conn=new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$id=$conn->quote(htmlentities($id));
$find=$conn->prepare("SELECT * FROM qs WHERE id=:id");
$find->bindParam(':id',$id);
$find->execute();
if($rows=$find->fetch(PDO::FETCH_ASSOC)){
$qs=$rows['question'];
$answerer=$rows['answerer'];
}
$answerer=explode("(",$answerer);
$ansgiver=$conn->prepare("SELECT * FROM ruhani WHERE name=:answerer");
$ansgiver->bindParam(':answerer',$answerer[0]);
$ansgiver->execute();
if($row=$ansgiver->fetch(PDO::FETCH_ASSOC)){
$name=$row['username'];
$avatar=$row['avatar'];
$sp=$row['sp'];
}
class chat {
public function fetchMessage() {
include('config.php');
global $id;
$conn=new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$ans=$conn->prepare("SELECT answere FROM ans WHERE q_id=:id ");
$ans->bindParam(':id',$id);
$ans->execute();
}
public function throwMessage($id, $text,$sayer){
include('config.php');
global $id;
$conn=new PDO("mysql:host=$servername;dbname=$dbname;charset=utf8;",$username,$password);
$conn->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$send=$conn->prepare("INSERT INTO ans(q_id,answere,sayer) VALUES(:q_id,:text,:sayer) ");
$send->bindParam(':q_id',$id);
$send->bindParam(':text',$text);
$send->bindParam(':sayer',$sayer);
$send->execute();
}
}
$chat = new chat();
?>
<!DOCTYPE html>
<head>
<title>Example Title</title>
<meta charset="utf-8">
<link rel="stylesheet" href="style/style.css" media="screen, projection" />
<script src="js/jquery-1.6.3.min.js"></script>
<script src="js/chat.js"></script>
</head>
<body>
<div class="all">
<div class="env">
<span><?php echo $name ?>|<?php echo $sp ?></span>
<span>Hi,Can I help you?</span><br>
<span>me:<?php echo $qs ?></span>
<div class="messages"></div>
</div>
<textarea name="ask_ans" class="ask_ans" id="ask_ans" placeholder="Please write here!"></textarea><br>
<input type="submit" class="submit" name="submit" value="send" />
<input type="hidden" id="hidden" value=<? echo $id ?> />
<input type="hidden" id="hidden2" value=<? echo $_SESSION['$username'] ?> />
</div>
</body>
<?php
}
?>
and this code for Ajax part(chat.js):
var chat = { }
chat.fetchMessage=function (){
$.ajax({
url:"send.php",
type: 'POST',
data: {method : 'fetch'},
cache:false,
success: function(data){
$(".all .env .messages").html(data)
}
});
}
chat.throwMessage=function (id,message,sayer){
if($.trim(message).length != 0 ){
$.ajax({
url:"send.php",
type: 'POST',
data: {method : 'throw' , id : id , message : message , sayer : sayer },
cache:false,
success: function(data){
chat.fetchMessage();
$(".ask_ans").val('');
}
});
}
}
chat.entry=$(".all .submit");
chat.entry.bind('click',function (evt){
evt.preventDefault();
chat.throwMessage($("#hidden").val(),$(".ask_ans").val(),$("#hidden2").val());
});
chat.interval=setInterval(chat.fetchMessage(),5000);
chat.fetchMessage()
And this is send.php:
<?php
require('chat.php');
if (isset($_POST['method']) and !empty($_POST['method'])){
$chat =new chat();
$method =trim($_POST['method']);
if ($method === 'fetch'){
$messages=$chat->fetchMessage();
if (!empty($messages)){
while($r=$messages->fetch(PDO::FETCH_ASSOC)){
$sayer=$r['sayer'];
if($sayer===$starter){
?>
<span class="text" style="float:left"><? echo $r['answere'] ?></span>
<?php
}else{
?>
<span class="text" style="float:right"><? echo $r['answere'] ?></span>
<?php
}
}
}
}else if ($method === 'throw'){
$message=trim(htmlentities($_POST['message']));
$id=trim(htmlentities($_POST['id']));
$sayer=trim(htmlentities($_POST['sayer']));
if(!empty($message) and !empty($id) and !empty($sayer)){
$chat->throwMessage($id,$message,$sayer);
}
}
}
?>
I am getting an error:
Undefined index:id
My problem is that I can't echo $qs
,$name
,$sp
in chat.php.
Can anyone understand the wrong part of my code?