Php 如何清理复选框?
我在谷歌上搜索了这个,也在Stackoverflow上搜索了答案。但是,对于如何清理复选框值,没有什么真正明确的答案 我理解,如果我的输入是:Php 如何清理复选框?,php,wordpress,validation,checkbox,sanitization,Php,Wordpress,Validation,Checkbox,Sanitization,我在谷歌上搜索了这个,也在Stackoverflow上搜索了答案。但是,对于如何清理复选框值,没有什么真正明确的答案 我理解,如果我的输入是: <input type="checkbox" value="submit_doors"> 我应该在复选框上运行所有这些检查吗?或者这是过度使用?首先,您需要为输入字段指定一个名称,以便可以从$\u POST变量访问它 假设您有以下输入字段,并希望对其进行清理 <input type="checkbox" name="door" val
<input type="checkbox" value="submit_doors">
我应该在复选框上运行所有这些检查吗?或者这是过度使用?首先,您需要为输入字段指定一个名称,以便可以从$\u POST变量访问它 假设您有以下输入字段,并希望对其进行清理
<input type="checkbox" name="door" value="submit_doors">
例如:如果要检查用户是否从复选框中提交值“submit_doors”,则可以使用如下功能
$sanitized_value = !empty($_POST['door']) ? prefix_sanitize_checkbox($_POST['door'], 'submit_doors') : ''; // this will return the value only if the checkbox contains the value "submit_doors", otherwise, it will return empty string.
这样,功能就灵活了。如果传递给第二个参数,它可以清除true false值和1以及其他字符串值
/**
* Sanitize checkbox
* @param int | string $input the input value to be sanitized
* @param int | string $expected_value The expected value
* @return int | string it returns the sanitize value of the checkbox.
*/
function prefix_sanitize_checkbox( $input, $expected_value=1 ) {
if ( $expected_value == $input ) {
return $expected_value;
} else {
return '';
}
}
$sanitized_value = !empty($_POST['door']) ? prefix_sanitize_checkbox($_POST['door'], 'submit_doors') : ''; // this will return the value only if the checkbox contains the value "submit_doors", otherwise, it will return empty string.