我一辈子都无法使用它,它是PHP。
<?php
if (!isset($_POST['ign']) || ($_POST['email'])) {
echo "Please enter all of the values!";
}
else {
echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is complete!";
}
?>
我也试过使用!isset两次。
isset()
接受的参数不止一个,因此只需传递需要检查的变量即可:
<?php
if (!isset($_POST['ign'], $_POST['email'])) {
echo "Please enter all of the values!";
}else{
echo "Thanks,". $_POST['ign'].", you will receive an email when the site is complete!";
}
?>
您也可以使用empty()
,但它一次不能接受多个变量。
这就是我解决这个问题的方法:
$expression = $_POST['ign'] || $_POST['email'] ;
if (!isset($expression) {
echo "Please enter all of the values!";
}
else {
echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is
complete!";
}
我所知道的最简单的方法:
<?php
if (isset($_POST['ign'], $_POST['email'])) {//do the fields exist
if($_POST['ign'] && $_POST['email']){ //do the fields contain data
echo ("Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is complete!");
}
else {
echo ("Please enter all of the values!");
}
}
else {
echo ("Error in form data!");
}
?>
编辑:更正代码以分别显示表单数据和空值错误。
说明:第一个if语句检查提交的表单是否包含ign和email两个字段。这样做是为了在根本没有传递ign或电子邮件的情况下阻止第二个if语句抛出错误(消息将打印到服务器日志中)。第二个if语句检查ign和email的值,看看它们是否包含数据。
试试这个:
<?php
if (!isset($_POST['ign']) || isset($_POST['email'])) {
echo "Please enter all of the values!";
}
else {
echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is complete!";
}
?>
isset($_POST['ign'],$_POST['email']));
然后检查空值。
您可以尝试以下代码:
<?php
if(!isset($_POST['ign'], $_POST['email'])) {
echo "Please enter all of the values!";
} else {
echo "Thanks, " . $_POST['ign'] . ", you will receive an email when the site is complete!";
}
?>
使用POST时,请使用empty()
。因为当你的表单发送数据时。空输入为async null!最好的方法是:
if ((!isset($_POST['ign']) || empty($_POST['ign'])) &&
(!isset($_POST['email']) || empty($_POST['email'])) {
是的!它很丑陋。。。
所以你可以使用:
<?php
if ( checkInput($_POST['ign']) || checkInput($_POST['email']) ) {
echo "Please enter all of the values!";
}
else {
echo "Thanks, " . $_POST['ign'] . ", you will recieve an email when the site is complete!";
}
function checkInput($input){
return ( !isset($input) || empty($input) );
}
?>
// if any of this session is set then
if (isset($_SESSION['tusername']) || isset($_SESSION['student_login'])) {
it will return true;
} else {
it will return false;
}