This should fix it
$(document).ready(function() {
var setInt = setInterval(function() {
$("#screen").load('messages.html');
}, 100);
});
function submitForm() {
$("#btn").click(function() {
var msg = $("#msg").val();
$.ajax({
type: 'POST' ,
url: 'process.php' ,
data: {"msg":msg},
datatype:'HTML'
success: function(result) {
$("#msg").val(' ');
alert(result);
}
});
});
return false;
}
$file = "messages.html";
if ($_POST) {
$msg = htmlspecialchars($_POST['msg']);
$user = "Derek".rand(0,100);
$old_content = file_get_content($file);
$post = "$user : $msg
\n";
$submission = "$post
$old_content";
$fp = fopen($file, 'w');
fwrite($fp, $submission);
fclose($fp);
}
?>
You were using a POST in your ajax call, and looking for a GET variable in your PHP, I also removed the redundant IF statement looking for action=='post', and removed the action parameter from being sent from the AJAX call. The 'msg' parameter is passed using the simple JSON. A bigger JSON might look like this:
{
'msg' : 'This is the message',
'name' : 'Batman!',
'email' : '[email protected]'
}
An important tip about this trick in IE8, you CANNOT leave a trailing apostrophe ( , ) on the end of the array or object or it will not work (technically IE8 is right this time, other browsers do overlook this syntax error however).
Also added result variable into the AJAX success function, the result variable will contain HTML spit out by the form handler page
(added datatype:'HTML' to ajax call). If you prefer to return a JSON to the AJAX call use datatype:'JSON' and in on your PHP page build an array to pass back and apply
echo json_encode($thearray); to return an array for javascript handling.
Untested, but should be damn close atleast, cheers!
Edited by JimmyJames, 04 January 2012 - 10:10 PM.