Probléme avec ajax svp
Solved
marwen109
Posted messages
81
Status
Membre
-
Groarh Posted messages 706 Status Membre -
Groarh Posted messages 706 Status Membre -
Bonjour,
J'ai créé un script de chat avec ajax et php(sans base de donnée , les messages sont stocké dans un fichier text avec php puis affiché)...
bref : mon script est la
*mettre votre message puis en clique sur change content "pas entré du clavier ca marche pas"
http://marwen.lockernerd.co.uk/
et le code :
et enfin un fichier chateur.txt initialement vide
mon probléme est que mon script poste des messages seulement localement sur mon pc,
je veux savoir pourquoi les message que j'ai posté ne sont pas transmis vers les autres internautes qui sont connecté a mon site web , et comment corrigé ca?
MERCI D'AVANCE :)
J'ai créé un script de chat avec ajax et php(sans base de donnée , les messages sont stocké dans un fichier text avec php puis affiché)...
bref : mon script est la
*mettre votre message puis en clique sur change content "pas entré du clavier ca marche pas"
http://marwen.lockernerd.co.uk/
et le code :
index.html
////////////////////////////////////////////
<html>
<head>
<style>
.button{background-color:black;border:2px orange;color:orange;height:25px;}
.input{border:1px black solid;height:25px;}
</style>
<script type="text/javascript">
function declanche(msg)
{
if(window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
req=new XMLHttpRequest();
}
else
{// code for IE6, IE5
req=new ActiveXObject("Microsoft.XMLHTTP");
}
req.onreadystatechange=function()
{
if (req.readyState == 4) {
if (req.status == 200) {
document.getElementById("myDiv1").innerHTML="";
document.getElementById("myDiv").innerHTML=document.getElementById("myDiv").innerHTML+req.responseText;}
else { alert("erreur par marwen");}
}
else
document.getElementById("myDiv1").innerHTML="<font color='red'size='5'>en attente....</font>";
}
req.open("GET","fiche.php?msg="+msg,true);
req.send();
}
</script>
</head>
<body>
<form name="f">
<input type="text"name="msg">
<button type="button" onclick="declanche(msg.value)"class="button">Change Content</button><br>
<div id="myDiv"><h2></h2></div>
<div id="myDiv1"><h2></h2></div>
</form>
</body>
</html>
/////////////////////////////////////
fiche.php
<?
//ouverture du fichier avec ajout a la fin
$x=fopen("chateur.txt","a+");
//récupération du message passé a travert ajax
$msg=$_GET['msg'];
//faire un retour a la ligne pour pouvoir lire chaque ligne
$msg="\n".$msg;
fwrite($x,$msg);
$ligne=file("chateur.txt");
//affichage le dernière ligne(message reçu)
echo $ligne[count($ligne)-1]."</br>";
//et fermuture du fichier
fclose($x);
?>
et enfin un fichier chateur.txt initialement vide
mon probléme est que mon script poste des messages seulement localement sur mon pc,
je veux savoir pourquoi les message que j'ai posté ne sont pas transmis vers les autres internautes qui sont connecté a mon site web , et comment corrigé ca?
MERCI D'AVANCE :)
3 answers
Hello,
oh wow, there are a lot of things going wrong. Alright. We'll try to make what you asked for work first, and then we'll look at the details :)
So, we have on hand:
- an HTML page that sends messages via Ajax
- a PHP script that records and returns the message.
The important word here is "returns": the server responds to a request, and only that one (that's the basic principle of the Web). In this case, it responds to the Ajax request that just sent the message.
You're missing half of the components needed to have a complete chat application: you only have the sending part; you need to create the receiving part.
The easiest solution to implement, which you will often find in various tutorials, is to have a second Ajax requester that polls the server regularly (every 2 or 3 seconds). I also recommend creating a second PHP script to properly differentiate things.
I'm not going to write you a tutorial; there are plenty of them online. I'd like to know if you can manage with the information I've just given you. If not, feel free to come back and ask me your questions, and I'll be happy to help you ;)
oh wow, there are a lot of things going wrong. Alright. We'll try to make what you asked for work first, and then we'll look at the details :)
So, we have on hand:
- an HTML page that sends messages via Ajax
- a PHP script that records and returns the message.
The important word here is "returns": the server responds to a request, and only that one (that's the basic principle of the Web). In this case, it responds to the Ajax request that just sent the message.
You're missing half of the components needed to have a complete chat application: you only have the sending part; you need to create the receiving part.
The easiest solution to implement, which you will often find in various tutorials, is to have a second Ajax requester that polls the server regularly (every 2 or 3 seconds). I also recommend creating a second PHP script to properly differentiate things.
I'm not going to write you a tutorial; there are plenty of them online. I'd like to know if you can manage with the information I've just given you. If not, feel free to come back and ask me your questions, and I'll be happy to help you ;)
Hello, I found your indication "interrogate the server regularly." It's just simple JavaScript.
<< setInterval("trigger('')",3000) >>
to trigger my ajax request regularly.........
My script works correctly today, I modified the page "fiche.php"
but will the shared file "chateur.txt"
cause a critical section, imagine both processes (the two chatters) accessing for writing at the same time..... can we use a semaphore in that case?
I think using a database is better than a text file],
anyway
Thank you for you :=)
<< setInterval("trigger('')",3000) >>
to trigger my ajax request regularly.........
My script works correctly today, I modified the page "fiche.php"
but will the shared file "chateur.txt"
cause a critical section, imagine both processes (the two chatters) accessing for writing at the same time..... can we use a semaphore in that case?
I think using a database is better than a text file],
anyway
Thank you for you :=)
Please extend my thanks to you. I will find a solution to this problem on my own
Don't worry about me :=)