在Ajax代码中,我有action = 'contact';
,在路由文件中使用了联系人:
Route::post('contact', array('uses' => 'FormsController@send', 'as' => 'post_form'));
在路由文件中,我有FormsController@send
是发送电子邮件的php文件:
$name = Input::get('name');
$getSubject = "Subject of my email";
$myEmail = 'myEmail@gmail.com';
$uid = md5(uniqid(time()));
$eol = "\r\n";
// header
$header = "From: " . $name . " <" . $email . ">\r\n";
$header .= "MIME-Version: 1.0" . $eol;
$header .= "Content-Type: multipart/mixed; boundary=\"" . $uid . "\"" . $eol;
$header .= "Content-Transfer-Encoding: 7bit" . $eol;
$header .= "This is a MIME encoded message." . $eol;
// message & attachment
$nmessage = "--" . $uid . "\r\n";
$nmessage .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
$nmessage .= "Content-Transfer-Encoding: 8bit" . $eol;
if($name != ''){$nmessage .= "Name: " . $name . "\r\n\r\n";}
// $nmessage .= "Wiadomość:\r\n" . $getMessage . "\r\n\r\n";
$nmessage .= "--" . $uid . "\r\n";
$nmessage .= "Content-Transfer-Encoding: base64\r\n";
$nmessage .= "--" . $uid . "--";
$send = mail($myEmail, $getSubject, $nmessage, $header);
Ajax定向到控制器文件并绕过表单,因此控制器文件不会从表单下载任何数据,并且无法发送邮件。我不知道如何将数据从表单传递到控制器文件。
我的Ajax:
const sendForm = function () {
action = 'contact';
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open('post', action, true);
xmlhttp.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
const getMessageSend = document.querySelector("#messageSend");
getMessageSend.innerText = "Thank you for sending an email. You will receive an answer shortly.";
} else {
const getMessageSendError = document.querySelector("#messageSendError");
getMessageSendError.innerText = "An error occurred and the email was not sent.";
}
};
// xmlhttp.open("post", action, true);
// xmlhttp.send();
const token = document.querySelector('meta[name="csrf-token"]').content;
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
xmlhttp.setRequestHeader('X-CSRF-TOKEN', token);
xmlhttp.send();
};
const sendMail = function() {
options.form.addEventListener('submit', function (e) {
e.preventDefault();
let validate = true;
const elementsRequired = document.querySelectorAll(":scope [formHr]");
[].forEach.call(elementsRequired, function(element) {
const type = element.type.toUpperCase();
if (type === 'TEXT') {
if (!validateText(element)) {validate = false;}
}
});
if (validate) {
sendForm();
// this.submit();
} else {
return false;
}
});
};
我的表单:
{!! Form::open(['action'=>['FormsController@send'], 'method' => 'post', 'class' => 'form', 'novalidate' => 'novalidate', 'files' => true]) !!}
<input type="text" name="name" placeholder="Name" formHr>
{!! Form::submit('Send', ['class' => 'submit-btn']); !!}
{!! Form::close() !!}
答案 0 :(得分:0)
我找到了解决方案。帮我这个主题Submit form laravel using AJAX
我只是将data
添加到我的代码中:
$.ajax({
type: "POST",
url: 'contact',
data: $(this).serialize(),
success: function () {
$("#messageSend").addClass("message-send");
$("#messageSend").text("Thank you for sending an email. You will receive an answer shortly.");
}
});
记住要使用完整版的Jquery!