Reputation: 1935
I am trying to reactive a Bootstrap modal automatically after a page refresh to show a feedback message, however my current code is not reloading the modal, nor showing the error messages. The page itself is reloading, and data submitted to the form is correctly updating in the database (or not submitting, depending on whether there is an error or not!), but the modal won't reopen after the page has refreshed.
My current code is below:
//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
}
UPDATE
To add more information, I believe that the reason the modal and error messages are not appearing is because, in order for the updated information to show on the page, the page itself has to be reloaded before the modal is reshown. I'm under the assumption that, on page reload, anything that's gone on prior to the reload is pretty much eradicated, hence why no information, such as the alert messages, are being shown when manually clicking the 'update profile' button to reopen the modal after the page has reloaded. Here's what should be happening:
Upvotes: 1
Views: 2910
Reputation: 8892
Per the Bootstrap documentation, the appropriate way to manually display a modal is to use the show
parameter like so:
$(function () {
$('#myModal').modal('show');
});
More info at http://twitter.github.com/bootstrap/javascript.html#modals
By wrapping the code in the jQuery constructor, the code will be executed when the DOM is ready.
Upvotes: 1