我在php脚本中有一个表单,它有2个字段(“year”和“web”)。按下提交后,表单的值存储在名为“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
}
?>
答案 0 :(得分:1)
您可以使用isset()
function和简单条件表达式的组合:
if ( isset( $_POST['web'] ) && $_POST['web'] != '' ){
// here you know know for certain that
// there is a non empty value in $_POST['web'].
}
答案 1 :(得分: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>';
}