jQuery POST to PHP hangs, causing unresponsive page
I have the following JavaScript on my page:
$('#report_submit').click(function(event){
event.preventDefault();
if ( $('#email').val() == "" )
{
reportmessage('Please enter an e-mail address.');
return false;
}
$.post('report.php',{method:reported,email:$开发者_JS百科('#email').val()},function(data){
if (data == 'error')
{
reportmessage('Catastrophic failure. Please try again.');
}
if (data == 'email')
{
reportmessage('Check your e-mail address and try again.');
}
if (data == 'success')
{
$('#reportform').fadeOut();
$('#reportsuccess').fadeIn();
return true;
}
});
});
And report.php:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
// Server side verification
$method = trim(strip_tags(stripslashes($_POST['method'])));
$email = trim(strip_tags(stripslashes($_POST['email'])));
if (empty($method) OR empty($email))
{
echo 'error';
exit;
}
if (filter_var($email, FILTER_VALIDATE_EMAIL) === false)
{
echo 'email';
exit;
}
// Write it to the log
$data = date('c').' - '.$_SERVER['REMOTE_ADDR'].' - '.$email.' - '.$method."\n";
file_put_contents ( 'reports.txt', $data, FILE_APPEND );
echo 'success';
exit;
}
?>
When the submit button is clicked, and there is a value in the e-mail address textfield, the page freezes almost instantly. I have to close/kill the process and reload the page. The JavaScript console shows nothing. I would assume the PHP script is running just fine; if I replace the script with echo 'error';
the page works and I get the catastrophic error message. However, passing the POST variables directly to the script and not through AJAX shows the correct output in the browser.
It can't be a PHP issue, and the JavaScript should work fine (and does, when the PHP script returns a simple value without doing anything else). I've tested this page on my Mac in Chrome and Firefox, and on Windows in Firefox, Chrome, and IE. All freeze.
Is this a server configuration issue, and if so, what should I look for or change?
精彩评论