单击确定页面刷新时带有成功消息的 codeIgniter Javascript 警报

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19684140/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 16:28:39  来源:igfitidea点击:

codeIgniter Javascript alert with success message on click ok page refresh

javascriptphpcodeigniterjquery

提问by modon

I have a code like this:

我有一个这样的代码:

<div class="alert alert-success">
    <a class="close" data-dismiss="alert">×</a>
    <?php 

        $message = "Your Upload was successful";
        if((isset($message))&&($message!='')){
        echo '<script> alert("'.str_replace(array("\r","\n"), '', $message).'");      </script>';
        }
       redirect($this->uri->uri_string());  //refresh page
    ?>

I want to show this success alert message and then if the user click on OK it will refresh the browser. In my case it is just refreshing the browser.

我想显示此成功警报消息,然后如果用户单击“确定”,它将刷新浏览器。就我而言,它只是刷新浏览器。

What will be the best way to do it.

最好的方法是什么。

Thanks a lot in advance.

非常感谢。

回答by anvoz

To make your code work as expected, you have to write the refresh function in Javascript instead of using PHP redirectfunction like the below:

为了使您的代码按预期工作,您必须在 Javascript 中编写刷新函数,而不是使用redirect如下所示的PHP函数:

<?php 
    $message = "Your Upload was successful";
    if ((isset($message)) && ($message != '')) {
        echo '<script>
            alert("'.str_replace(array("\r","\n"), '', $message).'");
            location.reload(true);
        </script>';
    }
?>

If you want to use Bootstrap modal, try this:

如果你想使用 Bootstrap 模式,试试这个:

<?php
$message = "Your Upload was successful";
if ((isset($message)) && ($message != '')):
?>
<div class="modal" id="alert-dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal">&times;</button>
                <h4 class="modal-title">Alert</h4>
            </div>
            <div class="modal-body">
                <?php echo $message; ?>
            </div>
            <div class="modal-footer">
                <button data-dismiss="modal" type="button" class="btn btn-primary">OK</button>
            </div>
        </div>
    </div>
</div>
<script>
$(function() {
    $('#alert-dialog').modal('show').on('hidden.bs.modal', function () {
        location.reload(true);
    });
});
</script>
<?php endif; ?>

回答by mic

I'm not sure this is possible with a standard alert box, you can do it with a confirm.

我不确定使用标准警报框是否可行,您可以通过确认来实现。

e.g.

例如

var con = confirm('Are you sure');

if (con == true) {
    //means the user clicked on `OK`
    //refresh the page
} else {
    //means the user clicked `Cancel`
}

Alternatively you could use a customized alert box, to find a suitable one just search on google.

或者,您可以使用自定义警报框,只需在谷歌上搜索即可找到合适的警报框。