页面刷新后自动激活Bootstrap模式

时间:2013-03-26 21:31:40

标签: php javascript twitter-bootstrap

我试图在页面刷新后自动响应一个Bootstrap模式以显示反馈消息,但是我当前的代码没有重新加载模态,也没有显示错误消息。页面本身正在重新加载,提交到表单的数据在数据库中正确更新(或者不提交,具体取决于是否存在错误!),但在刷新页面后模式不会重新打开。

我目前的代码如下:

//Clean
$submit = clean_string($_POST['submit']);

$id = $profile_info['id'];
$db_password = $profile_info['password'];


//Update account details
if ($submit == 'Save changes') {
    $first_name = clean_string($_POST['first-name']);
    $last_name = clean_string($_POST['last-name']);
    $email = clean_string($_POST['email']);
    $current_password = clean_string($_POST['current-password']);
    $new_password = clean_string($_POST['new-password']);
    $confirm_new_password = clean_string($_POST['confirm-new-password']);

    //Output variables
    $updateProfile_bad_message = '';
    $updateProfile_good_message = '';

    if ($db_server) {
        if (!empty($first_name)) {
            if ($first_name = clean_string($first_name)) {
                $query = "UPDATE users SET first_name = '$first_name' WHERE id = '$id'";
                mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>';
            } else {
                $updateProfile_bad_message = '<div class="alert alert-error">Sorry, something\'s gone wrong! Please try again later.</div>';?>
                <script type="text/javascript">
                    $('a.account-update').trigger('click');
                </script><?php
            }
        } 
        if (!empty($last_name)) {
            if ($last_name = clean_string($last_name)) {
                $query = "UPDATE users SET last_name = '$last_name' WHERE id = '$id'";
                mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>';
            } else {
                $updateProfile_bad_message = '<div class="alert alert-error">Sorry, something\'s gone wrong! Please try again later.</div>';?>
                <script type="text/javascript">
                    $('a.account-update').trigger('click');
                </script><?php
            }
        } 
        if (!empty($email)) {
            if ($email = clean_string($email)) {
                $taken = mysql_query("SELECT email FROM users WHERE email='$email'");
                $count = mysql_num_rows($taken);
                if ($count > 0) {
                    $updateProfile_bad_message = '<div class="alert alert-error">The email you have entered is already associated with a Screening account. Please choose another.</div>';
                } else if ($count = 0) {
                    $query = "UPDATE users SET email = '$email' WHERE id = '$id'";
                    mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                    $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>';
                } else {
                    $updateProfile_bad_message = '<div class="alert alert-error">Sorry, something\'s gone wrong! Please try again later.</div>';?>
                    <script type="text/javascript">
                        $('a.account-update').trigger('click');
                    </script><?php
                }
            }
        } 
        if ($_FILES) {
            $file_name = $_FILES['profile-image']['name'];
            $file_size = $_FILES['profile-image']['size'];
            $file_tmp_name = $_FILES['profile-image']['tmp_name'];


            //Determine filetype
            switch ($_FILES['profile-image']['type']) {
                case 'image/jpeg': $ext = "jpg"; break;
                case 'image/png': $ext = "png"; break;
                default: $ext = ''; break;
            }

            if ($ext) {
                //Check filesize
                if ($file_size < 50000000000) {
                    //Process file - resize, clean up filename and move to safe location
                    $image = new SimpleImage();
                    $image->load($file_tmp_name);
                    $image->resizeToWidth(250);
                    $image->save($file_tmp_name);


                    $n = "$file_name";
                    $n = ereg_replace("[^A-Za-z0-9.]", "", $n);
                    $n = strtolower($n);
                    $n = "avatars/$n";
                    move_uploaded_file($file_tmp_name, $n);
                    $query = "UPDATE users SET image = '$n' WHERE id = '$id'";
                    mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                    $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>';
                } else {
                    $updateProfile_bad_message = '<div class="alert alert-error">Please ensure your chosen file is less than 5MB.</div>';?>
                    <script type="text/javascript">
                        $('a.account-update').trigger('click');
                    </script><?php

                }
            } else {
                $updateProfile_bad_message = '<div class="alert alert-error">Please ensure your image is of filetype .jpg or .png.</div>';?>
                <script type="text/javascript">
                    $('a.account-update').trigger('click');
                </script><?php
            }
        }
        if (!empty($current_password)) {
            $current_password = clean_string($current_password);
            if ($current_password = md5($db_password)) {
                if ($new_password == $confirm_new_password) {
                    $new_password = clean_string($new_password);
                    $confirm_new_password = clean_string($confirm_new_password);
                    $new_password = md5($new_password);
                    $query = "UPDATE users SET password = '$new_password' WHERE id = '$id'";
                    mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
                    $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>';
                } else {
                    $updateProfile_bad_message = '<div class="alert alert-error">Your passwords did not match. Please check your spelling and try again.</div>';?>
                    <script type="text/javascript">
                        $('a.account-update').trigger('click');
                    </script><?php
                }
            } else {
                $updateProfile_bad_message = '<div class="alert alert-error">Your current password is incorrect. Please check your spelling and try again.</div>';?>
                <script type="text/javascript">
                    $('a.account-update').trigger('click');
                </script><?php
            }
        }
    } else {
        $updateProfile_bad_message = '<div class="alert alert-error">Error: could not connect to the database. Please try again shortly.</div>';?>
        <script type="text/javascript">
            $('a.account-update').trigger('click');
        </script><?php
    }
    require_once("db_close.php");?>
    <script type="text/javascript">
        window.location = "profile.php"
    </script>
    <script type="text/javascript">
        $('a.account-update').trigger('click');
    </script><?php
}

更新

要添加更多信息,我认为模态和错误消息未出现的原因是,为了在页面上显示更新的信息,必须在重新显示模式之前重新加载页面本身。我假设,在页面重新加载时,重新加载之前发生的任何事情都已被彻底根除,因此当手动单击“更新配置文件”按钮重新打开时,为什么没有显示警报消息等信息页面重新加载后的模态。这是应该发生的事情:

  • 如果有任何错误,只显示错误反馈消息,则无需重新加载页面
  • 如果没有错误,请重新加载页面以显示更新的信息并重新显示模式以确认更改已成功保存。

1 个答案:

答案 0 :(得分:1)

根据Bootstrap文档,手动显示模态的适当方法是使用show参数,如下所示:

$(function () {
  $('#myModal').modal('show');
});

http://twitter.github.com/bootstrap/javascript.html#modals

的更多信息

通过将代码包装在jQuery构造函数中,代码将在DOM准备好时执行。