Support success and error callbacks in rx_ajax submissions using iframe

This commit is contained in:
Kijin Sung 2021-09-13 10:57:55 +09:00
parent f3918d792a
commit 1f1e01917e
2 changed files with 19 additions and 13 deletions

View file

@ -972,24 +972,21 @@ class ModuleHandler extends Handler
// Handle iframe form submissions.
if(isset($_POST['_rx_ajax_form']) && starts_with('_rx_temp_iframe_', $_POST['_rx_ajax_form']))
{
$script = '';
if(!$oModule->toBool())
$data = [];
if ($this->error)
{
$script .= sprintf('window.parent.alert(%s);', json_encode($oModule->getMessage()));
$data['error'] = -1;
$data['message'] = lang($this->error);
}
else
{
if($oModule->getMessage() && $oModule->getMessage() !== 'success')
{
$script .= sprintf('window.parent.rhymix_alert(%s, %s);', json_encode($oModule->getMessage()), json_encode($oModule->getRedirectUrl()));
}
if($oModule->getRedirectUrl())
{
$script .= sprintf('window.parent.redirect(%s);', json_encode($oModule->getRedirectUrl()));
}
$data['error'] = $oModule->error;
$data['message'] = lang($oModule->message);
}
$data = array_merge($data, $oModule->getVariables());
ob_end_clean();
echo sprintf('<html><head></head><body><script>%s window.parent.remove_iframe(%s);</script></body></html>', $script, json_encode($_POST['_rx_ajax_form']));
echo sprintf('<html><head></head><body><script>parent.XE.handleIframeResponse(%s, %s);</script></body></html>', json_encode(strval($_POST['_rx_ajax_form'])), json_encode($data));
return;
}

View file

@ -351,7 +351,16 @@
$('<iframe id="' + iframe_id + '" name="' + iframe_id + '" style="display:none"></iframe>').appendTo($(document.body));
form.attr('method', 'POST').attr('enctype', 'multipart/form-data').attr('target', iframe_id);
form.find('input[name=_rx_ajax_form]').val(iframe_id);
window.remove_iframe = function(iframe_id) {
window.XE.handleIframeResponse = function(iframe_id, data) {
if (data.error) {
if (callback_error) {
callback_error(data);
} else {
alert(data.message);
}
} else {
callback_success(data);
}
if (iframe_id.match(/^_rx_temp_iframe_[0-9]+$/)) {
$('iframe#' + iframe_id).remove();
}