Php 做这件事的更好方法?除了很多“如果其他”的情况
我刚刚开始学习php、javascript等…尝试使用ajax创建一个成员系统,今天刚刚完成,我检查了一遍,发现我只使用if-else语句来编写所有代码…有更好的方法吗 member-update.js member\u update\u validation.php member-update.js ajax发布到member\u update\u validation.php,获取$response\u数组['status'],并根据状态运行一些jquery代码……这只是更新代码,我还有4个几乎相同的文件用于登录和注册……除了if-else语句之外,有人能告诉我正确的方法吗?JavaScript改进 您可以减少常见部分,例如错误总是附加到错误对话框,然后调用更新显示警告方法。此外,无论消息的类型如何,都会将其添加到元素中,然后调用一个方法:Php 做这件事的更好方法?除了很多“如果其他”的情况,php,javascript,if-statement,Php,Javascript,If Statement,我刚刚开始学习php、javascript等…尝试使用ajax创建一个成员系统,今天刚刚完成,我检查了一遍,发现我只使用if-else语句来编写所有代码…有更好的方法吗 member-update.js member\u update\u validation.php member-update.js ajax发布到member\u update\u validation.php,获取$response\u数组['status'],并根据状态运行一些jquery代码……这只是更新代码,我还有4个
success: function(msg){
var el, next
// Looks up the real message text in the
// object literal containing status labels
// for the current localization
, message = L10n[msg.status]
// Looks up the type of the message in a
// object literal that maps the message
// to a type regardless of locale
, type = types[msg.status];
if(message && type) {
// If the type is an error, use the error dialog box
// and the showWarning function
if(type === "error") {
el = $("#error_dialog_box");
next = update_showWarning;
// If the type is a success, use the success dialog box
// and the showSuccess function
} else if(type === "success") {
el = $("#success_dialog_box");
next = update_showSuccess;
} else if(type === "successRelog")
el = $("#success_dialog_box");
next = update_showSuccessRelog;
}
// The message type was found and a
// label exists for the current locale
// display the message
if(el && next) {
el.html(message);
next();
}
}
},
然后,您将维护一个对象文字,将msg.status映射到标签,并将msg.status映射到类型:
var L10n =
{ "error" : "xyz"
, "cookie_error" : "abc"
/*...*/ };
var types = {
{ "error" : "error"
/*...*/ };
我认为@Bergi的方法可能更好,因为在这种方法中,总是有可能忘记将类型添加到类型数组中
PHP修改
检查表单数据以将其分配给变量或以其他方式保留为空的构造:
if(!empty($_POST['update_contact'])){
$contact = mysql_real_escape_string($_POST['update_contact']);
}else{
$contact = '';
}
可以成为:
// The default value is blank
$contact = '';
// This is only overwritten if the update_contact field
// was posted and non empty
if(!empty($_POST['update_contact']))
$contact = mysql_real_escape_string($_POST['update_contact']);
这样可以将默认值分组在一起
我很想将输入、收集、验证和数据库交互分离到各自的方法中
您还可以对所有输入变量使用assoc数组,以便更轻松地对其进行操作:
// Default values, if they are all blank by default
// you could just loop over an array of keys instead
$data = array(
"update_contact" => ""
, "update_bday" => ""
, "password" => ""
, "update_password" => ""
, "update_confirm_password" => ""
);
// The mysql_real_escape_string function is applied
// to all of the fields that were posted (it would be
// appropriate to filter the $_POST array to fields
// we are interested in first) and then these modified
// values are merged into the default values
$data = array_merge($data, array_map(mysql_real_escape_string, $_POST));
JavaScript改进
您可以减少常见部分,例如错误总是附加到错误对话框,然后调用更新显示警告方法。此外,无论消息的类型如何,都会将其添加到元素中,然后调用一个方法:
success: function(msg){
var el, next
// Looks up the real message text in the
// object literal containing status labels
// for the current localization
, message = L10n[msg.status]
// Looks up the type of the message in a
// object literal that maps the message
// to a type regardless of locale
, type = types[msg.status];
if(message && type) {
// If the type is an error, use the error dialog box
// and the showWarning function
if(type === "error") {
el = $("#error_dialog_box");
next = update_showWarning;
// If the type is a success, use the success dialog box
// and the showSuccess function
} else if(type === "success") {
el = $("#success_dialog_box");
next = update_showSuccess;
} else if(type === "successRelog")
el = $("#success_dialog_box");
next = update_showSuccessRelog;
}
// The message type was found and a
// label exists for the current locale
// display the message
if(el && next) {
el.html(message);
next();
}
}
},
然后,您将维护一个对象文字,将msg.status映射到标签,并将msg.status映射到类型:
var L10n =
{ "error" : "xyz"
, "cookie_error" : "abc"
/*...*/ };
var types = {
{ "error" : "error"
/*...*/ };
我认为@Bergi的方法可能更好,因为在这种方法中,总是有可能忘记将类型添加到类型数组中
PHP修改
检查表单数据以将其分配给变量或以其他方式保留为空的构造:
if(!empty($_POST['update_contact'])){
$contact = mysql_real_escape_string($_POST['update_contact']);
}else{
$contact = '';
}
可以成为:
// The default value is blank
$contact = '';
// This is only overwritten if the update_contact field
// was posted and non empty
if(!empty($_POST['update_contact']))
$contact = mysql_real_escape_string($_POST['update_contact']);
这样可以将默认值分组在一起
我很想将输入、收集、验证和数据库交互分离到各自的方法中
您还可以对所有输入变量使用assoc数组,以便更轻松地对其进行操作:
// Default values, if they are all blank by default
// you could just loop over an array of keys instead
$data = array(
"update_contact" => ""
, "update_bday" => ""
, "password" => ""
, "update_password" => ""
, "update_confirm_password" => ""
);
// The mysql_real_escape_string function is applied
// to all of the fields that were posted (it would be
// appropriate to filter the $_POST array to fields
// we are interested in first) and then these modified
// values are merged into the default values
$data = array_merge($data, array_map(mysql_real_escape_string, $_POST));
通过使用对象文字在消息之间切换,可以大大缩短ajax回调中的if语句:
var errors = {
'error': '出錯,請再嘗試',
'cookie_error': '帳號信息出錯,請重新登入',
'captcha_error': '請正確輸入驗證碼',
'username_error': '請別嘗試修改用戶名',
'name_error': '請輸入暱稱,1-10個字符',
'email_empty': '電子郵件地址不能為空',
'email_format_error': '電子郵件地址格式錯誤',
'email_taken': '輸入的電子郵件地址已被使用',
'password_error': '密碼錯誤!不可使用符號或中文!6-40字符',
'update_password_error': '新密碼錯誤!不可使用中文符號或中文!6-40字符',
'update_password_not_match': '新密碼與確認新密碼不一致',
'update_wrong_password': '當前密碼輸入錯誤,請再次確認'
};
var successes = {
'update_success_all': '已成功更改個人資料,請重新登入',
'update_success': '已成功更改個人資料'
};
var status = msg.status;
if (status in errors) {
$("#error_dialog_box").html(errors[status]);
update_showWarning(); // maybe you can avoid this function call and inline it
} else if(status in successes) {
$("#success_dialog_box").html(successes[status]);
update_showSuccess(); // maybe you can avoid this function call and inline it
}
至少,您应该使用else if语句,以便在找到您的状态时停止与所有其他可能性进行比较。您可以通过使用对象文本在消息之间切换,大大缩短ajax回调中的if语句:
var errors = {
'error': '出錯,請再嘗試',
'cookie_error': '帳號信息出錯,請重新登入',
'captcha_error': '請正確輸入驗證碼',
'username_error': '請別嘗試修改用戶名',
'name_error': '請輸入暱稱,1-10個字符',
'email_empty': '電子郵件地址不能為空',
'email_format_error': '電子郵件地址格式錯誤',
'email_taken': '輸入的電子郵件地址已被使用',
'password_error': '密碼錯誤!不可使用符號或中文!6-40字符',
'update_password_error': '新密碼錯誤!不可使用中文符號或中文!6-40字符',
'update_password_not_match': '新密碼與確認新密碼不一致',
'update_wrong_password': '當前密碼輸入錯誤,請再次確認'
};
var successes = {
'update_success_all': '已成功更改個人資料,請重新登入',
'update_success': '已成功更改個人資料'
};
var status = msg.status;
if (status in errors) {
$("#error_dialog_box").html(errors[status]);
update_showWarning(); // maybe you can avoid this function call and inline it
} else if(status in successes) {
$("#success_dialog_box").html(successes[status]);
update_showSuccess(); // maybe you can avoid this function call and inline it
}
至少,您应该使用else if语句,以便在找到您的状态时停止与所有其他可能性进行比较。第一个代码片段的问题是:您通常希望从代码中提取接口消息,以便可以轻松编辑和翻译它们。然后,您可以使用键引用每条消息,并利用这些键简化控制流程:
var messages = {
error: '出錯,請再嘗試',
cookie_error: '帳號信息出錯,請重新登入',
...
};
function onUpdateSuccess(msg) {
var errorMessage = messages[msg.status];
$("#success_dialog_box").html(errorMessage);
update_showSuccessRelog();
}
稍后,您可以在服务器端生成messages对象,将其存储在数据库中,使其依赖于当前选择的站点语言等
在服务器端,您可能希望分离验证逻辑并使其可重用:
class Validate {
public static function length($input, $min, $max) {
$length = strlen($input);
return $length >= min && $length <= $max;
}
public static function notEmpty($input) {
return !empty($input);
}
public static function email($input) {
return filter_var($input, FILTER_VALIDATE_EMAIL);
}
...
}
第一个片段是一个问题:您通常希望从代码中提取接口消息,以便可以轻松地编辑和翻译它们。然后,您可以使用键引用每条消息,并利用这些键简化控制流程:
var messages = {
error: '出錯,請再嘗試',
cookie_error: '帳號信息出錯,請重新登入',
...
};
function onUpdateSuccess(msg) {
var errorMessage = messages[msg.status];
$("#success_dialog_box").html(errorMessage);
update_showSuccessRelog();
}
稍后,您可以在服务器端生成messages对象,将其存储在数据库中,使其依赖于当前选择的站点语言等
在服务器端,您可能希望分离验证逻辑并使其可重用:
class Validate {
public static function length($input, $min, $max) {
$length = strlen($input);
return $length >= min && $length <= $max;
}
public static function notEmpty($input) {
return !empty($input);
}
public static function email($input) {
return filter_var($input, FILTER_VALIDATE_EMAIL);
}
...
}
您可以使用switch case,但使用else if语句没有什么错。您可以创建一个数组,例如消息,其中键为msg.status,值为真实消息。然后您可以只使用$error_dialog_box.htmlmessages[msg.status];更新显示警告;当然,您可以先检查密钥是否存在,等等。正如@enenen提到的,您可以使用数组。另一个好处是,你可以很容易地制作一个英文或其他版本。你的代码可读性很好,没有什么可以改进的,我不考虑学习阶段的性能。即使它稍微减慢了处理速度,在实践中您也不会感觉到。您可以使用switch case,但使用else if语句没有什么错。您可以创建一个数组,例如密钥为ms的消息
g、 状态和值是真正的消息。然后您可以只使用$error_dialog_box.htmlmessages[msg.status];更新显示警告;当然,您可以先检查密钥是否存在,等等。正如@enenen提到的,您可以使用数组。另一个好处是,你可以很容易地制作一个英文或其他版本。你的代码可读性很好,没有什么可以改进的,我不考虑学习阶段的性能。即使这个过程稍微慢了一点,你在实践中也不会感觉到。+1我喜欢这种类型的方法+1根据我所接受的答案,一个更简单、更容易理解的书面答案。谢谢,我把我的js改成这样+1我喜欢这种类型+1的方法,根据我的说法,这是一个更简单、更容易理解的书面答案。谢谢,我把我的js改成了这样。。。仍然需要一些时间来理解这些代码,感谢您的指导。。。仍然需要一些时间来理解这些代码,感谢您的指导