是否可以在链接切换打开模态时传递变量?

时间:2014-11-30 05:05:49

标签: php

是否可以在链接中传递变量来切换模态?

我想用$ _GET或$ _POST

来做

数据切换='模式'数据目标='#myModal30' href =

1 个答案:

答案 0 :(得分:0)

不,所有PHP服务器处理都是在页面加载到客户端之前完成的。你需要AJAX来实现这一点,然后填充模态并启动它。

最终,这取决于您的要求,无论您是希望在页面加载之前调用还是在页面加载之后调用它。

使用Twitter Bootstrap 3.0 - 我使用会话变量在页面加载之前调用模态并用我自己的

替换默认的javascript alert()函数

设置模态的示例

$_SESSION['_Modal'] = "Success!|##|Your changes have been successfully applied!";
header("Location: page/they/came/from.php");
die();

“/page/they/came/from.php”的内容

if (isset($_SESSION['_Modal'])) {
    $msg = addslashes($_SESSION['_Modal']);
    echo '<script>alert("'.$msg.'");</script>';
    unset($_SESSION['_Modal']);
}

Javascript for alert()替换:

function alert(msg) {

    $('#aModal').remove();

    var parts = msg.split("|##|");

    var modal = '<div class="modal fade" id="aModal" tabindex="-1" role="dialog" aria-labelledby="aModalLabel" aria-hidden="true">' +
                    '<div class="modal-dialog">' +
                        '<div class="modal-content">' +
                            '<div class="modal-header">' +
                                '<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>' +
                                '<h4 class="modal-title" id="aModalLabel">' + parts[0] + '</h4>' +
                            '</div>' +
                            '<div class="modal-body">' + parts[1] + '</div>' +
                            '<div class="modal-footer">' +
                                '<button class="btn btn-primary" data-dismiss="modal">Close</button>' +
                            '</div>' +
                        '</div>' +
                    '</div>' +
                '</div>';

    $('body').prepend(modal);
    $('#aModal').modal({
        //backdrop: 'static',
        //keyboard: false
    });


}

我还替换了javascript confirm()函数

示例链接

<a data-href='go/here/if/confirmed.php' data-confirm='Are you sure you wish to delete this?'>

confirm()替换的Javascript代码

$('[data-confirm]').click(function(ev) {
    var href = $(this).attr('data-href');

    if (!$('#dataConfirmModal').length) {
        $('body').append('<div id="dataConfirmModal" class="modal fade" role="dialog" aria-labelledby="dataConfirmLabel" aria-hidden="true"><div class="modal-dialog"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button><h4 id="dataConfirmLabel">Please Confirm</h4></div><div class="modal-body"></div><div class="modal-footer"><button class="btn" data-dismiss="modal" aria-hidden="true">Cancel</button><a class="btn btn-primary" id="dataConfirmOK">OK</a></div></div></div></div>');
    } 

    $('#dataConfirmModal').find('.modal-body').html($(this).attr('data-confirm'));
    $('#dataConfirmOK').attr('href', href);
    $('#dataConfirmOK').click(function() {
        $('#dataConfirmModal').fadeOut(300);
        return true;
    })
    $('#dataConfirmModal').modal({
        backdrop: 'static',
        keyboard: false
    });
    return false;
});