2014-09-02 68 views
-1

如何使用PHP来检查表单字段的特定值?如何使用PHP来检查表单字段的特定值?

需要插入值'7',否则表单字段将无效;与所有其他号码,以便无效,那么7

if(empty($_POST['captcha'])) 
{ 
    $this->add_error("Did you solve the captcha?"); 
    $ret = false; 
} 

作品以检查是否有任何价值 - 但我需要指定,必须是7

if(strlen($_POST['captcha']) =!7) // tried this but form completely died or disappeared 
{ 
    $this->add_error("Did you solve the captcha?"); 
    $ret = false; 
} 
+0

嘿@Jack这就是答案,男人。如果你想发布 – 2014-09-03 00:12:52

回答

2

这里要注意两点:

  1. ,则不应使用empty()在这里,因为"0"被认为是空的,以及和它看起来像它可能是一个方程式的答案;请使用isset()而不是仅检查该值是否为请求的一部分。

  2. 您正试图检查$_POST['captcha']的长度,而不是比较其值。

所以:

if (!isset($_POST['captcha']) || $_POST['captcha'] != 7) { 
    $this->add_error('Did you solve the captcha?'); 
    $ret = false; 
}