PDA

Դիտել ողջ տարբերակը : PHP AJAX



Dawson
03.08.2016, 21:29
Բարև ձեզ, ժողովուրդ կօգնեք ,ոչ մի կերպ չեմ հասկանում որտեղա սխալը,

Չատ եմ գրում,նորմալ աշխատումա մի քանի էջում ,իսկ մնացածում չի աշխատում,ավելի կոնկրետ, ՕԿ սեղմելուց հետո պետքա բազայում գրանցի նոր տող,
Ajax ին էտքանել լավ չեմ տիրապետում, բայց իմ գրած օրինակում հասկանում եմ որը ինչ դերա տանում .

Էս էլ կոդը:



<?php
$agent = $_SERVER['HTTP_USER_AGENT'];


if(stristr($agent, 'UBrowser')) $agent = 'UBrowser'; //Opera?
elseif(stristr($agent, 'MSIE')) $agent = 'IE'; //Internet exploler?
elseif(stristr($agent, 'Firefox')) $agent = 'Firefox'; //Mozilla?
// elseif(stristr($agent, 'Chrome')) $agent = 'Chrome'; //Mozilla?


?>

<style type="text/css">




#aside1 {

position: fixed;
z-index: 101;
right:20%;
bottom:0;
width:150px;
height:30px;
background:black;
opacity:0.85;
color:#fff;
cursor:pointer;
padding-top:8px;
text-align: center;
display:block;
}

#mychatbox{
position: fixed;
z-index: 101;
right:16%;
bottom:0;
height:410px;
width:270px;
background:black;
opacity:0.87;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
display:none;
}

#mychattop{
width:270px;
position: absolute;
top:0;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
background:black;
font-size:25px;
color:#fff;
text-align: right;
font-weight:bold;
padding-right: 7%;
padding-top:3%;
padding-bottom: 2%;
display:block;
cursor:pointer;
border-bottom:1px solid #353434;

}

#chattext{

position: absolute;
bottom:15%;
background:#191616;
width:266px;
height:78%;
margin:2px;
overflow-y: scroll;
overflow-x: hidden;
}


.onemessage{
<?php if($agent=='Firefox')
echo 'font-weight:bolder;';
?>
font-size:13px;
background:black;
color:white;
opacity: 1;
display: table;
width:250px;
margin:1px;
text-align: left;
margin-bottom: 2px;
padding:5px;
}

#avelacnel{
font-weight:bolder;
position: absolute;
bottom:0;
/*background:red;
*/height:18%;
width:266px;
margin:2px;
padding-left: 10px;
}

#chatmes{
float:left;
resize: none;
background:#fbfbfb;
opacity:0.92;
color:black;

margin-top:20px;
border:none;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
#chatbut{
margin-top:20px;


<?php if($agent=='Firefox')
echo 'height:47px;';
else
echo 'height:49px;' ?>
color:white;
background: #dc0909;

border:none;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}

</style>


<script type="text/javascript">

function cuctal(){
var cht=document.getElementById("aside1");
cht.style.display='none';
var chtbox=document.getElementById("mychatbox");
chtbox.style.display='block';
}


function taqcnel(){
var cht=document.getElementById("aside1");
cht.style.display='block';
var chtbox=document.getElementById("mychatbox");
chtbox.style.display='none';
}

function getdetails(){
var chatmessage = $('#chatmes').val();
var usrname = $('#usernamme').val();
var usrid = $('#userid').val();
$.ajax({
type: "POST",
url: "chat/send",
data: {chatmes:chatmessage, usname:usrname , usid:usrid},
async: true
});
$('#chatmes').val('');
}

// setInterval(function(){
// $("#chattext").load("chat/view#chattext");
// }, 6000);



</script>

<!-- START CHAT -->
<div onclick="cuctal()" id="aside1">Չատ
</div>
<div id="mychatbox">
<div onclick="taqcnel()" id="mychattop">-</div>
<div id="chattext">

<?php require_once (ROOT.'/view/chat/chat.php');
?>

</div>



<div id="avelacnel">



<?php if(isset($_SESSION['user'])) : ?>

<textarea <?php if($agent=='Firefox') echo 'rows="2" cols="20"';elseif($agent=='UBrowser') echo 'rows="3" cols="24"'; else echo ' rows="3" cols="26"'; ?> type="text" id="chatmes" name="message" /></textarea>
<input type="hidden" name="" id="usernamme" value="<?php echo $user['username']; ?>" />
<input type="hidden" name="" id="userid" value="<?php echo $user['id']; ?>" />

<input type="submit" id="chatbut" onclick="getdetails()" value="OK">
<?php else : ?><br>
<a style="font-size:12px;color:white;" href="/login">Մուտք գործեք կամ գրանցվեք չատից օգտվելու համար </a>
<?php endif ?>

</div>
</div>
<!-- END CHAT -->


Էս տեղադրվածա ֆուտերում,



public function actionSend()

{




$result=false;




$chatmes=htmlentities($_POST['chatmes']);


$usid=$_POST['usid'];
$ch_date=date_default_timezone_set('Asia/Yerevan');
$ch_date=date("m.d.y"." / "."H:i");
$usname=$_POST['usname'];

if(strlen($chatmes)>=2)
{
$db = Db::getConnection();
if(!$db) {echo "vuavduvuda";die;}

$sql = 'INSERT INTO chat (chatmes,usid,ch_date,usname) VALUES ("'.$chatmes.'","'.$usid.'","'.$ch_date.'","'.$usname.'")';
$result = $db->prepare($sql);
$result->execute();



}



return true;

}


Էսել սերվերի մասնա

ԿանաչԸնծա
03.08.2016, 22:41
Ողջույն։

Խառը բան եք արել, գոնե առանձնացրեք CSS, JS ու HTML, որ ավելի հեշտ լինի նայել հասկանալ ինչ ա կատարվում։

Dawson
04.08.2016, 10:00
լավ


CSS

<?php
$agent = $_SERVER['HTTP_USER_AGENT'];


if(stristr($agent, 'UBrowser')) $agent = 'UBrowser'; //Opera?
elseif(stristr($agent, 'MSIE')) $agent = 'IE'; //Internet exploler?
elseif(stristr($agent, 'Firefox')) $agent = 'Firefox'; //Mozilla?
// elseif(stristr($agent, 'Chrome')) $agent = 'Chrome'; //Mozilla?


?>

<style type="text/css">




#aside1 {

position: fixed;
z-index: 101;
right:20%;
bottom:0;
width:150px;
height:30px;
backgroundack;
opacity:0.85;
color:#fff;
cursor:pointer;
padding-toppx;
text-align: center;
displayock;
}

#mychatbox{
position: fixed;
z-index: 101;
right:16%;
bottom:0;
height:410px;
width:270px;
backgroundack;
opacity:0.87;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
display:none;
}

#mychattop{
width:270px;
position: absolute;
top:0;
border-top-right-radius: 10px;
border-top-left-radius: 10px;
backgroundack;
font-size:25px;
color:#fff;
text-align: right;
font-weight:bold;
padding-right: 7%;
padding-top:3%;
padding-bottom: 2%;
displayock;
cursor:pointer;
border-bottom:1px solid #353434;

}

#chattext{

position: absolute;
bottom:15%;
background:#191616;
width:266px;
height:78%;
margin:2px;
overflow-y: scroll;
overflow-x: hidden;
}


.onemessage{
<?php if($agent=='Firefox')
echo 'font-weight:bolder;';
?>
font-size:13px;
backgroundack;
color:white;
opacity: 1;
display: table;
width:250px;
margin:1px;
text-align: left;
margin-bottom: 2px;
padding:5px;
}

#avelacnel{
font-weight:bolder;
position: absolute;
bottom:0;
/*background:red;
*/height:18%;
width:266px;
margin:2px;
padding-left: 10px;
}

#chatmes{
float:left;
resize: none;
background:#fbfbfb;
opacity:0.92;
colorack;

margin-top:20px;
border:none;
border-top-left-radius: 5px;
border-bottom-left-radius: 5px;
}
#chatbut{
margin-top:20px;


<?php if($agent=='Firefox')
echo 'height:47px;';
else
echo 'height:49px;' ?>
color:white;
background: #dc0909;

border:none;
border-top-right-radius: 5px;
border-bottom-right-radius: 5px;
}

</style>


Javascript

<script type="text/javascript">

function cuctal(){
var cht=document.getElementById("aside1");
cht.style.display='none';
var chtbox=document.getElementById("mychatbox");
chtbox.style.display='block';
}


function taqcnel(){
var cht=document.getElementById("aside1");
cht.style.display='block';
var chtbox=document.getElementById("mychatbox");
chtbox.style.display='none';
}

function getdetails(){
var chatmessage = $('#chatmes').val();
var usrname = $('#usernamme').val();
var usrid = $('#userid').val();
$.ajax({
type: "POST",
url: "chat/send",
data: {chatmes:chatmessage, usname:usrname , usid:usrid},
async: true
});
$('#chatmes').val('');
}

// setInterval(function(){
// $("#chattext").load("chat/view#chattext");
// }, 6000);



</script>



HTML


<!-- START CHAT -->
<div onclick="cuctal()" id="aside1">Չատ
</div>
<div id="mychatbox">
<div onclick="taqcnel()" id="mychattop">-</div>
<div id="chattext">

<?php require_once (ROOT.'/view/chat/chat.php');
?>

</div>



<div id="avelacnel">



<?php if(isset($_SESSION['user'])) : ?>

<textarea <?php if($agent=='Firefox') echo 'rows="2" cols="20"';elseif($agent=='UBrowser') echo 'rows="3" cols="24"'; else echo ' rows="3" cols="26"'; ?> type="text" id="chatmes" name="message" /></textarea>
<input type="hidden" name="" id="usernamme" value="<?php echo $user['username']; ?>" />
<input type="hidden" name="" id="userid" value="<?php echo $user['id']; ?>" />

<input type="submit" id="chatbut" onclick="getdetails()" value="OK">
<?php else : ?><br>
<a style="font-size:12px;color:white;" href="/login">Մուտք գործեք կամ գրանցվեք չատից օգտվելու համար </a>
<?php endif ?>

</div>
</div>
<!-- END CHAT -->