feat(web/user/edit): set ajax submit

This commit is contained in:
Baoshuo Ren 2022-10-20 10:21:07 +08:00
parent 3985b97f2e
commit 8977eb263d
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -160,9 +160,31 @@ EOD);
} }
DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle', github = '$esc_github', website = '$esc_website', avatar_source = '$esc_avatar_source' WHERE username = '{$user['username']}'"); DB::update("UPDATE user_info SET email = '$esc_email', qq = '$esc_qq', sex = '$esc_sex', motto = '$esc_motto', codeforces_handle = '$esc_codeforces_handle', github = '$esc_github', website = '$esc_website', avatar_source = '$esc_avatar_source' WHERE username = '{$user['username']}'");
header('Content-Type: application/json');
die(json_encode(['status' => 'success']));
}; };
$update_profile_form->submit_button_config['margin_class'] = 'mt-3'; $update_profile_form->submit_button_config['margin_class'] = 'mt-3';
$update_profile_form->submit_button_config['text'] = '更新'; $update_profile_form->submit_button_config['text'] = '更新';
$update_profile_form->setAjaxSubmit(<<<EOD
function(res) {
if (res.status === 'success') {
$('#result-alert')
.html('个人信息修改成功!')
.addClass('alert-success')
.removeClass('alert-danger')
.show();
} else {
$('#result-alert')
.html('个人信息修改失败。' + (res.message || ''))
.removeClass('alert-success')
.addClass('alert-danger')
.show();
}
$(window).scrollTop(0);
}
EOD);
$update_profile_form->runAtServer(); $update_profile_form->runAtServer();
} elseif ($cur_tab == 'password') { } elseif ($cur_tab == 'password') {
if (isset($_POST['submit-change_password']) && $_POST['submit-change_password'] == 'change_password') { if (isset($_POST['submit-change_password']) && $_POST['submit-change_password'] == 'change_password') {
@ -269,6 +291,7 @@ EOD);
<?php if ($cur_tab == 'profile'): ?> <?php if ($cur_tab == 'profile'): ?>
<div class="card"> <div class="card">
<div class="card-body"> <div class="card-body">
<div id="result-alert" class="alert" role="alert" style="display: none"></div>
<?php $update_profile_form->printHTML() ?> <?php $update_profile_form->printHTML() ?>
</div> </div>
</div> </div>
@ -314,6 +337,8 @@ EOD);
$('#form-change_password').submit(function() { $('#form-change_password').submit(function() {
var ok = true; var ok = true;
$('#result-alert').hide();
ok &= getFormErrorAndShowHelp('current_password', validatePassword); ok &= getFormErrorAndShowHelp('current_password', validatePassword);
ok &= getFormErrorAndShowHelp('new_password', validateSettingPassword); ok &= getFormErrorAndShowHelp('new_password', validateSettingPassword);
@ -339,6 +364,8 @@ EOD);
.addClass('alert-danger') .addClass('alert-danger')
.show(); .show();
} }
$(window).scrollTop(0);
}, },
error: function() { error: function() {
$('#result-alert') $('#result-alert')
@ -346,6 +373,8 @@ EOD);
.removeClass('alert-success') .removeClass('alert-success')
.addClass('alert-danger') .addClass('alert-danger')
.show(); .show();
$(window).scrollTop(0);
} }
}); });
} }
@ -418,6 +447,8 @@ EOD);
</form> </form>
<script> <script>
$('#form-privilege').submit(function(e) { $('#form-privilege').submit(function(e) {
$('#result-alert').hide();
$.post('', { $.post('', {
user_type: $('input[name=user_type]:checked').val(), user_type: $('input[name=user_type]:checked').val(),
problem_uploader: $('input[name=problem_uploader]').prop('checked') ? 'yes' : 'no', problem_uploader: $('input[name=problem_uploader]').prop('checked') ? 'yes' : 'no',
@ -432,12 +463,16 @@ EOD);
.addClass('alert-success') .addClass('alert-success')
.removeClass('alert-danger') .removeClass('alert-danger')
.show(); .show();
$(window).scrollTop(0);
} else { } else {
$('#result-alert') $('#result-alert')
.html('权限修改失败。' + (res.message || '')) .html('权限修改失败。' + (res.message || ''))
.removeClass('alert-success') .removeClass('alert-success')
.addClass('alert-danger') .addClass('alert-danger')
.show(); .show();
$(window).scrollTop(0);
} }
}); });