【发布时间】:2015-08-10 03:56:28
【问题描述】:
我的网站上有联系表。它将消息发送到电子邮件。我尝试在不使用 AJAX 重新加载页面的情况下执行此操作,但 AJAX 似乎不起作用:消息已发送但页面仍重定向到 call-form.php。我的代码中有什么不正确的地方? (包括jQuery)
HTML
<form name="freeCall" action="<?php bloginfo(template_url); ?>/mail/call-form.php" method="post" class="popover-form" id="free-call-form">
<label for="name1">Name</label><span class="pull-right close">×</span><input placeholder="Name" name="call-name" type="text" id="name1" >
<label for="phone">Phonenumber</label><input name="phone" type="text" value="" placeholder="+375" id="phone" >
<input type="submit" value="Call me back" >
</form>
PHP - call-form.php
<?
if((isset($_POST['call-name']))&&(isset($_POST['phone'])&&$_POST['phone']!="")){
$to = 'test@gmail.com';
$subject = 'Callback';
$message = '
<html>
<head>
<title>Call me back</title>
</head>
<body>
<p><b>Name:</b> '.$_POST['call-name'].'</p>
<p><b>Phonenum:</b> '.$_POST['phone'].'</p>
</body>
</html>';
$headers = "Content-type: text/html; charset=utf-8 \r\n";
$headers .= "From: Site <info@mail.com>\r\n";
mail($to, $subject, $message, $headers);
}
?>
JS
$(function () {
$("#free-call-form").submit(function () {
var form_data = $(this).serialize();
$.ajax({
type: "POST",
url: "call-form.php",
data: form_data,
success: function () {
alert("It's OK!");
}
});
});
});
【问题讨论】:
-
action="<?php bloginfo(template_url); ?>/mail/call-form.php"在 AJAX 中是没用的 -
完全没用? php文件是不必要的??
-
你忘记了一些事情,比如你的ajax返回。我正在写一个有效的答案
标签: javascript php jquery ajax forms