0

我在 php 脚本中有一个表单,它有 2 个字段(“年份”和“网络”)。按下提交后,表单的值存储在名为“save_job”的数组中。如果用户将“web”字段留空,我只想让我的脚本打印一条警告消息。这是我的代码:

<?php  

if(empty($_POST) === false){

 if(  empty($errors) === true  ){

        //displays error message if year is not a number

        if( preg_match("&[^0-9s ]&",$_POST['year']) ){
            $errors[]='<font color="#963">Year must contain 0-9 characters and spaces</font>';
        } preg_replace("&[^0-9s ]&","",$_POST['year']); 


        }
 }

?>


    <form   action=""   method="POST"  >   


    <?php 
        if ( isset($_GET['success']) === true && empty($_GET['success'])===true ){
            echo'</br><h3>Section "My News" Updated Sucessfuly!</h3>';?>
            <?php 
        }else{
                if( empty($_POST) === false  &&  empty($errors) === true ){
                    $save_job = array(
                                        'year' => $_POST['year'],
                                        'web' => $_POST['web'],
                                        'user_id' => $session_user_id,
                                        );

                            //checks if "web" field in $save_job array is empty     

                            if(!empty($save_job['web']) === true ){
                                echo $save_job['web'] ;
                            } else{
                                echo"web field is empty";
                            }




                }else if ( empty($errors) === false ){
                    echo output_errors($errors);
                    echo'</br></br>';
                }

                ?>

    Year<input name="year" type="text" size="2" />

    Web Site<input name="web" type="text" size="50" />

<input type="submit" value="" name="submit"   />

</form> 

<?php

}

?>
4

2 回答 2

1

您可以结合使用该isset()函数和一个简单的条件表达式:

if ( isset( $_POST['web'] ) && $_POST['web'] != '' ){
  // here you know know for certain that 
  // there is a non empty value in $_POST['web'].
}
于 2013-10-28T15:56:04.060 回答
1
                        if(!empty($save_job['web']) === true ){
                            echo $save_job['web'] ;
                        } else{
                            echo"web field is empty";
                            $errors[] = <font color="#963">Web field is empty</font>';
                        }
于 2013-10-28T16:27:52.613 回答