I’m having a little problem getting my data to submit. I’m trying to make a simple chat box using PHP and Ajax, but whenever I try to submit data it will only post after it has been submitted several times. I’m hoping somebody could tell me the problem with me code.
I’m a very novice coder and this is my first time using this site so be nice if its an obvious mistake ^^”
The main chatbox:
<head>
<link href="CSS.css" rel="stylesheet" type="text/css">
<script type="text/javascript">
function sendmessage()
{
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
var name=encodeURIComponent(document.getElementById("name").value);
var message=encodeURIComponent(document.getElementById("message").value);
xmlhttp.open("POST","insert.php",true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("name="+name+"&message="+message);
}
</script>
</head>
<body>
<center>
<table width="600">
<tr>
<td height="400">
<center>
<iframe src ="output.php" width="580px" height="386px">
<p>Your browser does not support iframes.</p>
</iframe>
</center>
</td>
</tr>
<tr>
<td>
<form method="POST"></br>
  Name:      
<input type="text" id="name" autocomplete="off" size="15"/><br/><br/>
  Message:  
<input type="text" id="message" autocomplete="off" size="70"/>  
<input type="submit" value="Send" onclick="sendmessage()"/>
</form>
</td>
</tr>
</table>
</center>
</body>
The PHP file which reads the input data and writes it to a log file:
<?php
$name='<table><tr><td width="100%">'.$_POST['name']." Says:</td>";
$message="<table><tr><td>".$_POST['message']."</td></tr></table></br>\n";
$time="<td>".date("d/m/y-G:i")."</td></tr></table>";
$log = "log.file";
$write = fopen($log, 'a') or die("Can't open file");
fwrite($write, $name);
fwrite($write, $time);
fwrite($write, $message);
fclose($fh);
?>
I just grabbed your code and tested it out in my computer. Did some small changes but everything if working fine. I just commented
xmlhttp.execCommand('mceRemoveControl',false,'content');then everything worked fine on FireFox.
Below you can see the code that I ran:
HTML:
PHP:
I just tried in FireFox 3.6 Safari 5 and Chrome 6. I’m using a Mac so I didn’t tried in IE. In those 3 browsers everything worked fine after the line that I told you at the beginning. Could you proved more details about your problem?