我试图在我的应用程序的多个区域成功重定向页面后显示Bootstrap警报,但我根本无法让它们显示。以下是我使用它们的区域的一些示例,以及用于初始化的代码。我认为一个解决方案可能是所有人的解决方案,因为它们都共享非常相似的显示方案。
示例1 - 成功删除列表后显示提醒
我在watchlists.php上有一个Bootstrap模式来删除一个特定的关注列表,并且在成功删除关注列表后,我将用户重定向回profile.php。我想在profile.php的顶部设置一条成功的警报消息,为用户提供已删除其关注列表的视觉反馈。我应该提一下,Watchlist本身确实删除了,它只是profile.php上没有显示的成功提示。
watchlists.php上的代码
// Delete Watchlist
if ($submit == 'Delete Watchlist') {
require_once("db_connect.php");
$deleteWatchlist_bad_message = '';
if ($db_server) {
$purge_watchlist_query = "DELETE FROM watchlist_films WHERE watchlist_id = " . $watchlist_id;
mysql_query($purge_watchlist_query) or die("Delete failed. " . mysql_error() . "<br />" . $purge_watchlist_query);
$delete_watchlist_query = "DELETE FROM watchlists WHERE watchlist_id = " . $watchlist_id ;
mysql_query($delete_watchlist_query) or die("Delete failed. " . mysql_error() . "<br />" . $delete_watchlist_query);
} else {
$deleteWatchlist_bad_message = '<div class="alert alert-error">Error: could not connect to the database.</div>';
}
require_once("db_close.php");?>
<script type="text/javascript">
window.location = "profile.php"
</script><?php
$deleteWatchlist_good_message = '<div class="alert alert-success">Watchlist successfully deleted</div>';
}
profile.php上的代码以显示提醒(已编辑为仅显示显示$deleteWatchlist_good_message
的部分)
<body>
<div class="container"><?php
require_once ("header.php");
require_once("profile-controller.php");?>
<?php echo $deleteWatchlist_good_message; ?>
<div class="well main-content">
<p class="page-title">Hello, <span class="bold"><?php echo $profile_info['first_name']; ?></span>. Here's your Screening profile.</p>
示例2 - 在模态刷新时显示警告
我试图在用户更新其个人资料信息后,在页面重新加载时酌情显示查询成功和查询失败警报。目前,配置文件信息完全按照应有的方式更新,但Bootstrap模式不会重新初始化,并且在页面重新加载后不会显示警报消息。
用于处理配置文件更新的profile.php上的代码
//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);
} 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);
} 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>';?>
<script type="text/javascript">
$(window).ready(function() {
$('#profileUpdate').modal('show');
});
</script><?php
} else if ($count = 0) {
$query = "UPDATE users SET email = '$email' WHERE id = '$id'";
mysql_query($query) or die("Insert failed. " . mysql_error() . "<br />" . $query);
} 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);
} 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);
} 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>
<?php $updateProfile_good_message = '<div class="alert alert-success">Changes saved</div>'; ?>
<script type="text/javascript">
$(function () {
$('a.account-update').modal('show');
});
</script><?php
}
profile.php上的代码,用于显示模态构造
<div id="profileUpdate" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="profileUpdateLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="profileUpdateLabel" class="modal-title">Update your Screening account details</h3>
</div>
<form name="profile-update" class="profile-update" action="profile.php" method='POST' enctype="multipart/form-data">
<div class="modal-body">
<?php echo $updateProfile_bad_message; ?>
<?php echo $updateProfile_good_message; ?>
<input type="text" class="input-block-level" name="first-name" alt="first-name" placeholder="<?php echo $profile_info['first_name']; ?>">
<input type="text" class="input-block-level" name="last-name" alt="last-name" placeholder="<?php echo $profile_info['last_name']; ?>">
<input type="file" class="profile-picture-upload" name="profile-image" alt="profile-image">
<input type="email" class="input-block-level" name="email" alt="email" placeholder="<?php echo $profile_info['email']; ?>">
<input type="password" class="input-block-level" name="current-password" alt="current-password" placeholder="Current Password">
<input type="password" class="input-block-level" name="new-password" alt="new-password" placeholder="New Password">
<input type="password" class="input-block-level" name="confirm-new-password" alt="confirm-new-password" placeholder="Confirm New Password">
</div>
<div class="modal-footer">
<button type="button" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<button type="submit" class="btn btn-success" name="submit" value="Save changes">Save changes</button>
</div>
</form>
</div>
为冗长的帖子道歉,我只想尽可能多地提供信息:)