php - Opencart + SweetAlert + Ajax。为什么我无法捕获 POST 请求?
问题描述
我尝试使用 3 个输入(姓名、电子邮件、文本)创建 SweetAlert 表单。此表单提交后必须将表单数据发送到管理员电子邮件。我做到了,一切正常!但我确信我的控制器代码无效。
CMS:OpenCart 2.3 + SweetAlert 2。
这是 SweetAlert 表单 + Ajax 请求:
<script type="text/javascript">
window.onload = function () {
var a = document.getElementById('director');
a.onclick = function() {
Swal({
type: 'warning',
title: 'Your message',
html:
'<input name="name" id="swal-input1" class="swal2-input" placeholder="Your name">' +
'<input name="email" id="swal-input2" class="swal2-input" placeholder="Email">' +
'<textarea name="text" id="swal-textarea1" class="swal2-textarea" placeholder="Enter your text..." style="display: flex;"></textarea>',
showCancelButton: true,
confirmButtonColor: '#ff5908',
cancelButtonColor: '#666',
confirmButtonText: 'Send',
cancelButtonText: 'Cancel',
preConfirm: function () {
return new Promise(function (resolve) {
resolve([
$('#swal-input1').val(),
$('#swal-input2').val(),
$('#swal-textarea1').val()
])
})
},
}).then(function (result) {
if (result.value) {
var result = {};
result.name = $('#swal-input1').val();
result.email = $('#swal-input2').val();
result.text = $('#swal-textarea1').val();
var data = JSON.stringify(result);
$.ajax({
url:"index.php?route=common/message/index",
type: "post",
data: data,
dataType: "json",
success: function() {Swal({type: 'success', text: 'Message sent'});},
error: function(xhr,status,error){
console.log(status);
console.log(error);
}
})
}
})
return false;
}
}
</script>
之后,ajax 请求被发送到“消息”控制器到“索引”方法。
<?php
class ControllerCommonMessage extends Controller {
public function index() {
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
$username = $data->name;
$email = $data->email;
$msg = $data->text;
$subject = 'Test email from: '.$email;
$mail = new Mail();
$mail->protocol = $this->config->get('config_mail_protocol');
$mail->parameter = $this->config->get('config_mail_parameter');
$mail->smtp_hostname = $this->config->get('config_mail_smtp_hostname');
$mail->smtp_username = $this->config->get('config_mail_smtp_username');
$mail->smtp_password = html_entity_decode($this->config->get('config_mail_smtp_password'), ENT_QUOTES, 'UTF-8');
$mail->smtp_port = $this->config->get('config_mail_smtp_port');
$mail->smtp_timeout = $this->config->get('config_mail_smtp_timeout');
$mail->setTo('mail@mail.com');
$mail->setFrom($this->config->get('config_email'));
$mail->setSender(html_entity_decode($this->config->get('config_name'), ENT_QUOTES, 'UTF-8'));
$mail->setSubject(html_entity_decode($subject, ENT_QUOTES, 'UTF-8'));
$mail->setHtml($msg);
$mail->send();
}
}
因此,表单正在工作,消息发送成功。但是,这部分代码我不喜欢:
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
我也试着这样写:
if (isset($this->request->post['data'])) {
$result = $this->request->post['data'];
var_dump($result);
} else {
echo 'empty';
}
但没有任何效果。“结果”为空。我做错了什么?
解决方案
推荐阅读
- javascript - 离开或刷新页面时如何弹出自定义模态?
- ios - 按钮 onclick 适用于 android 但不适用于 ios
- javascript - 在不修改原始数组的情况下将数组复制和计算到另一个数组
- keyboard - AQTime 重新映射标准键盘快捷键?
- android - 在 Android Auto 中镜像现有的 android 应用程序
- java - 尝试从 Saucelabs 中的 android 模拟器调用 chrome 浏览器时无法创建新会话?
- elasticsearch - Python elasticsearch-dsl doctype 更新超时
- google-sheets - 将数据从谷歌表格导入谷歌表单
- html - 带有 HAML 的 Rails 中的响应式导航栏
- reactjs - 构建错误发生 webpack 配置