我可以發送電子郵件並正常獲取,但想要單擊提交按鈕,表單已發送並向用戶顯示一條消息,並繼續在頁面上正常顯示。 Ja試着用ajax和一切都沒有。發送電子郵件並繼續在同一頁面
我希望消息出現在div中,與下圖完全相同。
HTML
<form class="well form-horizontal" action="php/contato.php" method="post" id="contact_form">
<fieldset>
<legend style="text-align: center;">Fale conosco</legend>
<!-- Nome -->
<div class="form-group">
<label class="col-md-4 control-label">Nome</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span>
<input name="name" placeholder="Nome Completo" class="form-control" type="text" required>
</div>
</div>
</div>
<!-- Email-->
<div class="form-group">
<label class="col-md-4 control-label">E-Mail</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-envelope"></i></span>
<input name="email" placeholder="E-Mail" class="form-control" type="text" required>
</div>
</div>
</div>
<!-- Telefone-->
<div class="form-group">
<label class="col-md-4 control-label">Telefones</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-earphone"></i></span>
<input name="telefone" placeholder="(xx)xxxxx-xxxx" class="form-control" type="text" required> </br>
<input name="celular" placeholder="Seu Whatsapp (Opcional)" class="form-control" type="text">
</div>
</div>
</div>
<!-- Assunto-->
<div class="form-group">
<label class="col-md-4 control-label">Assunto</label>
<div class="col-md-4 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-comment"></i></span>
<input name="assunto" placeholder="Assunto" class="form-control" type="text" required>
</div>
</div>
</div>
<!-- Mensagem -->
<div class="form-group">
<label class="col-md-4 control-label">Deixe-nos sua mensagem.</label>
<div class="col-md-6 inputGroupContainer">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" name="mensagem" placeholder="Dúvidas, elogios, Criticas ou Sugestões são bem vindas." required></textarea>
</div>
</div>
</div>
**<!-- I want the message to appear here -->**
<div class="alert alert-success" role="alert" id="success_message">Sucesso <i class="glyphicon glyphicon-thumbs-up"></i> Muito obrigado pelo contato, retornaremos em breve.</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label"></label>
<div class="col-md-4">
<button type="submit" class="btn btn-warning">Enviar <span class="glyphicon glyphicon-send"></span></button>
</div>
</div>
</fieldset>
</form>
的JavaScript
$(function() {
$('#contact_form').validator();
$('#contact_form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "php/contato.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data)
{
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact_form').find('.messages').html(alertBox);
$('#contact_form')[0].reset();
}
}
});
return false;
}
})
});
contato.php
<?php
// configure
$from = 'email';
$sendTo = 'email';
$subject = 'Contato do site';
$fields = array('name' => 'Nome do Cliente', 'email' => 'Email', 'telefone' => 'Telefone', 'celular' => 'Celular', 'assunto' => 'Assunto', 'mensagem' => 'Mensagem'); // array variable name => Text to appear in email
$okMessage = 'Email enviado, já já entraremos em contato';
$errorMessage = 'Ocorreu um erro no envio e ja fomos notificados sobre isso, aperte F5 e tente novamente por favor';
// let's do the sending
try
{
$emailText = "Mais um contato chegando através do site\n=============================\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
mail($sendTo, $subject, $emailText, "From: " . $from);
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
catch (\Exception $e)
{
$responseArray = array('type' => 'danger', 'message' => $errorMessage);
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
}
else {
echo $responseArray['message'];
}
?>
阿賈克斯救援...... – Poiz
無法測試不知道哪個插件用於'validator()'。所以,檢查錯誤並查看您的控制檯。不知道如果jquery的加載與否。 –