未选择某些内容时阻止通知

时间:2014-03-08 14:05:10

标签: php notice

我使用异常错误构建一个检查,以便在没有选择值时通知它。他显示了该消息,但他也说没有选择值(在通知中),我该如何防止该错误?这是关于名称标签'距离。

PHP

if(isset($_POST["btnCreateGroup"])){
        try {
        $group = new Group();
        $group->GroupDistance = mysql_real_escape_string($_POST["distance"]);
        $group->GroupName = mysql_real_escape_string($_POST["groupname"]);
        //$group->Groupduration = mysql_real_escape_string($_POST["name"]);
        $group->CreateGroup();

        } catch(exception $e) {
        $feedback = $e -> getMessage();
    }
}


public function __set($p_sProperty, $p_vValue)
        {
            switch($p_sProperty)
            {
                case "GroupName":
                if(!empty($p_vValue))
                {
                    $this->m_sGroupName = $p_vValue;
                }
                else
                {
                    throw new Exception("Ow! Give me a name to your group");
                }
                break;

                case "GroupDistance":
                if(!empty($p_vValue))
                {
                    $this->m_iGroupDistance = $p_vValue;
                }
                else
                {
                    throw new Exception("Ow! Give me a distance to run, please");
                }
                break;



            }
        }

HTML

<select name="distance">
                        <option selected="selected" disabled="disabled" value="none">Distance</option>
                        <option value="1000">1000 meter</option>
                        <option value="1500">1500 meter</option>
                        <option value="2000">2000 meter</option>
                        <option value="2500">2500 meter</option>
                    </select>

1 个答案:

答案 0 :(得分:2)

只需检查是否设置了值,如果没有,请改为使用默认值:

$distance  = (isset($_POST["distance"]))  ? $_POST["distance"]  : '';
$groupname = (isset($_POST["groupname"])) ? $_POST["groupname"] : '';
$name      = (isset($_POST["name"]))      ? $_POST["name"]      : '';

$group->GroupDistance = mysql_real_escape_string($distance);
$group->GroupName     = mysql_real_escape_string($groupname);
$group->Groupduration = mysql_real_escape_string($name);