我已经写了这样的功能,现在它返回“你正在运行一个全面的广告活动”,即使我没有选择任何方框或只选择一两个。我假设问题出在if语句中,我是否需要运行isset两次?
function adMix () {
$tv = (isset($_POST['TV']));
$radio = (isset($_POST['Radio']));
$search = (isset($_POST['Search']));
$mobile = (isset($_POST['Mobile']));
$mail = (isset($_POST['Mail']));
if ($tv || $radio || $search || $mobile || $mail) {
echo "You are running a comprehensive ad campaign.";
} else {
echo "You can do more to market your dealership.";
}
}
答案 0 :(得分:0)
尝试使复选框表现为一个数组,将它们全部重命名为mktg[]
,例如:
<p>Marketing Mix:</p>
<input type="checkbox" name="mktg[]" value="TV">TV<br>
<input type="checkbox" name="mktg[]" value="Radio">Radio<br>
<input type="checkbox" name="mktg[]" value="Search">Search<br>
<input type="checkbox" name="mktg[]" value="Mobile">Mobile<br>
<input type="checkbox" name="mktg[]" value="Mail">Direct Mail<br>
<br>
功能:
function adMix(){
if($_POST){
if(!empty($_POST['mktg'])){
if(count($_POST['mktg'])) echo "You are running a comprehensive ad campaign.";
}else echo "You can do more to market your dealership.";
}
}
这样,if(count($_POST['mktg']))
验证是否选中了任何选项并返回true。如果你需要定义必须选择多少选项才能使竞选成为现实,让我们说3,就这样做:
if(count($_POST['mktg']) >= 3)
答案 1 :(得分:0)
function adMix () {
if (isset($_POST['Tv']) || isset($_POST['Radio']) || isset($_POST['Search']) || isset($_POST['Mobile']) || isset($_POST['Mail'])) {
$tv = isset($_POST['Tv'])?$_POST['Tv']:""; //You can access the index only if it's set, else you will be shown a notice
$radio = isset($_POST['Radio'])?$_POST['Radio']:"";
$search = isset($_POST['Search'])?$_POST['Search']:"";
$mobile = isset($_POST['Mobile'])?$_POST['Mobile']:"";
$mail = isset($_POST['Mail'])?$_POST['Mail']:"";
echo "You are running a comprehensive ad campaign.";
} else {
echo "You can do more to market your dealership.";
}
}
答案 2 :(得分:-3)
试试这个:
function adMix () {
@$tv = $_POST['TV'];
@$radio = $_POST['Radio'];
@$search = $_POST['Search'];
@$mobile = $_POST['Mobile'];
@$mail = $_POST['Mail'];
if (isset($tv) || isset($radio) || isset($search) || isset($mobile) || isset($mail)) {
echo "You are running a comprehensive ad campaign.";
} else {
echo "You can do more to market your dealership.";
}
}