I tried to create SweetAlert form with 3 inputs (name,email,text). This form, after submitting must to send Form Data to admin email. I have made it and everything work! But I am sure that my controller code is not valid.
CMS: OpenCart 2.3 + SweetAlert 2.
Here is SweetAlert form + Ajax request:
<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>
After that, ajax request is sending to "Message" controller to "index" method.
<?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();
}
}
So, the form is working, messages sending successfully. But, this section of code I don't like:
$request_body = file_get_contents('php://input');
$data = json_decode($request_body);
I also tried to write it like this:
if (isset($this->request->post['data'])) {
$result = $this->request->post['data'];
var_dump($result);
} else {
echo 'empty';
}
But nothing works. "result" is empty. What I am doing wrong?