私はphp、javascriptなどの学習を始めたばかりです....ajaxを使用してメンバーシステムを実行しようとしていますが、今日完了したばかりで、もう一度チェックすると、すべてのコードを実行するためにif elseステートメントのみを使用していることがわかりました.. .そうするより良い方法はありますか?
member-update.js
$('#member_update_form').submit(false);
function check_member_update(){
$.ajax({
type: "POST",
url: "../handlers/member_update_validation.php?do=update",
data: $("#member_update_form").serialize(),
dataType: "json",
success: function(msg){
if(msg.status == 'error'){
$("#error_dialog_box").html('出錯,請再嘗試');
update_showWarning();
}
if(msg.status == 'cookie_error'){
$("#error_dialog_box").html('帳號信息出錯,請重新登入');
update_showWarning();
}
if(msg.status == 'captcha_error'){
$("#error_dialog_box").html('請正確輸入驗證碼');
update_showWarning();
}
if(msg.status == 'username_error'){
$("#error_dialog_box").html('請別嘗試修改用戶名');
update_showWarning();
}
if(msg.status == 'name_error'){
$("#error_dialog_box").html('請輸入暱稱,1-10個字符');
update_showWarning();
}
if(msg.status == 'email_empty'){
$("#error_dialog_box").html('電子郵件地址不能為空');
update_showWarning();
}
if(msg.status == 'email_format_error'){
$("#error_dialog_box").html('電子郵件地址格式錯誤');
update_showWarning();
}
if(msg.status == 'email_taken'){
$("#error_dialog_box").html('輸入的電子郵件地址已被使用');
update_showWarning();
}
if(msg.status == 'password_error'){
$("#error_dialog_box").html('密碼錯誤!不可使用符號或中文!6-40字符');
update_showWarning();
}
if(msg.status == 'update_password_error'){
$("#error_dialog_box").html('新密碼錯誤!不可使用中文符號或中文!6-40字符');
update_showWarning();
}
if(msg.status == 'update_password_not_match'){
$("#error_dialog_box").html('新密碼與確認新密碼不一致');
update_showWarning();
}
if(msg.status == 'update_wrong_password'){
$("#error_dialog_box").html('當前密碼輸入錯誤,請再次確認');
update_showWarning();
}
if(msg.status == 'update_success_all'){
$("#success_dialog_box").html('已成功更改個人資料,請重新登入');
update_showSuccessRelog();
}
if(msg.status == 'update_success'){
$("#success_dialog_box").html('已成功更改個人資料');
update_showSuccess();
}
},
error: function(){
document.getElementById('update_captcha_img').src = '/securimage/securimage_show.php?' + Math.random();
alert('未知錯誤,請聯絡管理員');
}
});
//make sure the form doesn't post
return false;
};
member_update_validation.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/securimage/securimage.php';
$securimage = new Securimage();
$response_array = array();
require $_SERVER['DOCUMENT_ROOT'] . '/common/db/main.db.php';
if(!empty($_GET['do']) && $_SESSION['loggedIn'] == 1 && isset($_COOKIE['logintoken'])){
if($_GET['do'] == 'update'){
if($_SESSION['logintoken'] != $_COOKIE['logintoken']){
$response_array['status'] = 'cookie_error';
}else{
$logintoken = $_COOKIE['logintoken'];
$database = new Database();
$database->query('SELECT * FROM member_info WHERE logintoken = :logintoken');
$database->bind(':logintoken',$logintoken);
$check_user = $database->single();
$username = mysql_real_escape_string(strtolower($check_user['username']));
$name = mysql_real_escape_string($_POST['update_name']);
$email = mysql_real_escape_string($_POST['update_email']);
if(!empty($_POST['update_contact'])){
$contact = mysql_real_escape_string($_POST['update_contact']);
}else{
$contact = '';
}
if(!empty($_POST['update_bday'])){
$bdate = mysql_real_escape_string($_POST['update_bday']);
}else{
$bdate = '';
}
$password = mysql_real_escape_string($_POST['password']);
if(!empty($_POST['update_password'])){
$update_password = mysql_real_escape_string($_POST['update_password']);
}else{
$update_password = '';
}
if(!empty($_POST['update_confirm_password'])){
$update_confirm_password = mysql_real_escape_string($_POST['update_confirm_password']);
}else{
$update_confirm_password = '';
}
$captcha = mysql_real_escape_string($_POST['update_captcha']);
unset($_POST);
if(empty($captcha) || $securimage->check($captcha) == false){
$response_array['status'] = 'captcha_error';
}
if($username != $check_user['username']){
$response_array['status'] = 'username_error';
}
if(strlen($name) < 1 || strlen($name) > 10){
$response_array['status'] = 'name_error';
}
if(empty($email)){
$response_array['status'] = 'email_empty';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE){
$response_array['status'] = 'email_format_error';
}
if($email != $check_user['email']){
$database = new Database();
$database->query('SELECT * FROM member_info WHERE email = :email');
$database->bind(':email',$email);
$exmail = $database->single();
if(!empty($exmail)){
$response_array['status'] = 'email_taken';
unset($exmail);
}
}
if(strlen($password) < 6 || strlen($password) > 40){
$response_array['status'] = 'password_error';
}
if(!empty($update_password) && !empty($update_confirm_password)){
if(strlen($update_password) < 6 || strlen($update_password) > 40){
$response_array['status'] = 'update_password_error';
}
if($update_password != $update_confirm_password){
$response_array['status'] = 'update_password_not_match';
}
}
if(empty($response_array['status'])){
$check_password = hash('sha256', $password . $check_user['salt']);
for($round = 0; $round < 65536; $round++){
$check_password = hash('sha256', $check_password . $check_user['salt']);
}
if($check_password === $check_user['password']){
$login_ok = true;
}else{
$login_ok = false;
$response_array['status'] = 'update_wrong_password';
}
if($login_ok == true){
if(!empty($update_password)){
$salt = dechex(mt_rand(0, 2147483647)) . dechex(mt_rand(0, 2147483647));
$update_password = hash('sha256', $update_password . $salt);
for($round = 0; $round < 65536; $round++){
$update_password = hash('sha256', $update_password . $salt);
}
$database = new Database();
$database->query('UPDATE member_info SET email = :email, name = :name, contact = :contact, birthdate = :bdate, password = :update_password, salt = :salt WHERE username = :username');
$database->bind(':username', $check_user['username']);
$database->bind(':email', $email);
$database->bind(':name', $name);
$database->bind(':contact', $contact);
$database->bind(':bdate', $bdate);
$database->bind(':update_password', $update_password);
$database->bind(':salt', $salt);
$database->execute();
$response_array['status'] = 'update_success_all';
}else{
$database = new Database();
$database->query('UPDATE member_info SET email = :email, name = :name, contact = :contact, birthdate = :bdate WHERE username = :username');
$database->bind(':username', $check_user['username']);
$database->bind(':email', $email);
$database->bind(':name', $name);
$database->bind(':contact', $contact);
$database->bind(':bdate', $bdate);
$database->execute();
$response_array['status'] = 'update_success';
}
}
}
}
}else{
$response_array['status'] = 'error';
}
unset($check_user);
echo json_encode($response_array);
unset($response_array);
return false;
}else{
header('Location: index.php');
}
?>
member-update.js
ajaxはに投稿しmember_update_validation.php
、バックを取得$response_array['status']
し、ステータスに応じていくつかのjqueryコードを実行します...これは単なる更新コードです。ログインと登録にほぼ同じファイルがさらに4つあります...誰かが私に正しい方法を教えてくれますかif else ステートメント以外?