我想用jQuery和php构建一个简单的sendmail表单。我试图用警报测试成功,但我什么都看不到。我认为我调用jQuery函数的方式有问题,因为我没有得到警报。这是我的代码:带有jQuery和PHP的sendmail不起作用
contact.js:
jQuery(function($) {'use strict',
var form = $('.contact-form');
alert ('Hello world');
form.submit(function() {'use strict',
$this = $(this);
$.post("sendemail.php", $(".contact-form").serialize(),function(result){
if(result.type == 'success'){
$this.prev().text(result.message).fadeIn().delay(3000).fadeOut();
}
});
return false;
});
});
sendmail.php
header('Content-type: application/json');
$status = array(
'type'=>'success',
'message'=>'Thank you for contact us. As early as possible we will contact you '
);
$name = @trim(stripslashes($_POST['name']));
$email = @trim(stripslashes($_POST['email']));
$subject = @trim(stripslashes($_POST['subject']));
$message = @trim(stripslashes($_POST['message']));
$email_from = $email;
$email_to = '[email protected]';//replace with your email
$body = 'Name: ' . $name . "\n\n" . 'Email: ' . $email . "\n\n" . 'Subject: ' . $subject . "\n\n" . 'Message: ' . $message;
$success = @mail($email_to, $subject, $body, 'From: <'.$email_from.'>');
echo json_encode($status);
die;
第一步是删除PHP中的所有符号,它们会压制任何可能帮助您调试的错误。 我猜你使用它们,因为你不想错误来打破你的JSON,这个问题应该包括如何做到这一点:http://stackoverflow.com/questions/15949304/turn-off-display-error- php-ini – ThomasRedstone
你已经写过''use strict',',但是你必须这样写:''use strict“;'。 它不关心你是否使用单引号或双引号。但是定义上你必须用分号';' – dlopez