Problem with ajax please

Solved
marwen109 Posted messages 81 Status Membre -  
Groarh Posted messages 706 Status Membre -
Hello,

Configuration: Windows XP / Safari 533.4

I created a chat script with AJAX and PHP (without a database, messages are stored in a text file with PHP and then displayed)...
anyway: my script is here

*enter your message then click on change content "typing isn't working"

http://marwen.lockernerd.co.uk/


and the 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("error by marwen");} } else document.getElementById("myDiv1").innerHTML="<font color='red'size='5'>waiting....</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 <? //opening the file for appending $x=fopen("chateur.txt","a+"); //getting the message passed through ajax $msg=$_GET['msg']; //adding a newline to be able to read each line $msg="\n".$msg; fwrite($x,$msg); $ligne=file("chateur.txt"); //displaying the last line (received message) echo $ligne[count($ligne)-1]."</br>"; //and closing the file fclose($x); ?>



and finally an initially empty file chateur.txt

my problem is that my script only posts messages locally on my PC,
I want to know why the messages I posted are not transmitted to other users connected to my website, and how to fix it?

THANK YOU IN ADVANCE :)

3 answers

Groarh Posted messages 706 Status Member 185
 
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 ;)
0
marwen109 Posted messages 81 Status Member 2
 
Firstly, thank you very much for your response :). I knew something like this was missing ("interrogating the server regularly....").

Please extend my thanks to you. I will find a solution to this problem on my own
Don't worry about me :=)
0
marwen109 Posted messages 81 Status Member 2
 
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 :=)
0
Groarh Posted messages 706 Status Member 185
 
Hello,
The server handles the parallelism for you; you don't have to worry about it.
0