drozwmi5440 2014-03-27 09:37
浏览 26

ctype_digit验证无法使用ajax传递0

I have the following situation:

<select name="n">
    <option>Please fill</option>
    <option value="0">0</option>
    <option value="1">1</option>
</select>

Now, i pass the serialized form with Ajax. I correctly see this in the console's POST panel:

[...]&n=0&[...]

My php validation script check both if the field has been filled and, for security reasons, that it is a digit:

parse_str($_REQUEST['data'], $values);
if (!ctype_digit($values['n'])) {
    array_push($err, 'n');
}

Can someone explain WHY if i'm passing the value 0 this check always fails? If i pass any other select option with a digit other than 0, it let me in... I tried also using is_numeric instead of ctype_digit (just to try) and it fails as well. But this makes no sense...as 0 is both numeric and digit!

UPDATE if i var_dump($values['n']), i get that it is null...so is there any problem with the parse_str($_REQUEST['data'], $values)?

  • 写回答

1条回答 默认 最新

  • douxin8383 2014-03-27 09:40
    关注

    Both 0 and '0' (as integer and as string) are considered false. So !$values['n'] is true. ctype_digit works just fine. You probably want to check for !isset($values['n']) instead of !$values['n'].

    评论

报告相同问题?