PHP if(isset)与2个无线电组和1个文本名不能正常工作

PHP if(isset)与2个无线电组和1个文本名不能正常工作,php,if-statement,isset,Php,If Statement,Isset,同样,我遇到的问题是,当文本字段或两个无线电组中的任何一个未选中时,它不会抛出“错误”消息,而是仅当所有3个字段都已填充时才会抛出它们。以下是整个html和PHP页面: <body> <form id="form1" name="form1" method="post" action="hw1.php"> <h1> <label for="name2"></label> Music Survey </h1> <p>

同样,我遇到的问题是,当文本字段或两个无线电组中的任何一个未选中时,它不会抛出“错误”消息,而是仅当所有3个字段都已填充时才会抛出它们。以下是整个html和PHP页面:

<body>
<form id="form1" name="form1" method="post" action="hw1.php">
<h1>
<label for="name2"></label>
Music Survey
</h1>
<p>Please Enter Your First Name
<label for="firstname"></label>
<input name="firstname" type="text" id="firstname" 
size="18"maxlength="18"  />
</p>
<h3>Please Enter Your Age Group</h3>
<p>
<label>
<input type="radio" name="agegroup" value="Youth" id="agegroup_0" />
Youth</label>(12 - 21)<br /><label>
<input type="radio" name="agegroup" value="Adult" id="agegroup_1" />
Adult</label>(22 - 50)<br /> <label>
<input type="radio" name="agegroup" value="Elderly" id="agegroup_2"/>
Elderly</label>(51 - Dead)
</p>
<h3>Please Enter Your Favorite Style Of Music</h3>
<p>
<label>
<input type="radio" name="music" value="Pop"  id="musicpreference_0" />
Pop</label>
<br />
<label>
<input type="radio" name="music" value="Country"    
id="musicpreference_1" />
Country</label>
<br />
<label>
<input type="radio" name="music" value="Rock" id="musicpreference_2" />
Rock</label>
</h3>
</h3>
<p>
<input type="submit" name="button" id="button" value="Submit Your Anwser" />
</p>
<p><br />
<input type="reset" name="button2" id="button2" value="Reset My Anwsers" 
/>
<br />
</p>
</form>
</body>

And here is the PHP portion:
<body>

<?php

if(isset($_POST['agegroup'])
    || isset($_POST['music']) 
    || !empty($_POST['firstname']))

    {
    echo "<h1>Please return to the form and fill out completely</h1>";
    }
    else
    {

 ?>
<h1>Thank For Taking the Survey <?php echo $_POST['firstname'] ?> </h1>

<?php

    if ($_POST['agegroup'] == 'Youth' && $_POST['music'] == 'Pop')
    {
        echo 'Pop Music Is Appropriate For Young People';
    }
    elseif ($_POST['agegroup'] == 'Youth' && $_POST['music'] == 'Rock')
    {
        echo 'Adults Listen To Rock Music, Not Young People';
    }
    elseif ($_POST['agegroup'] == 'Youth' && $_POST['music'] == 'Country')
    {
        echo "Country Music Is For Your Grandparents";
    }


    if ($_POST['agegroup'] == 'Adult' && $_POST['music'] == 'Pop')
    {
        echo 'Adults Don\'t Listen To Pop Music, Their Kids Do ';
    }
    elseif ($_POST['agegroup'] == 'Adult' && $_POST['music'] == 'Rock')
    {
        echo 'Rock Music Is Just Right For Adults';
    }
    elseif ($_POST['agegroup'] == 'Adult' && $_POST['music'] == 'Country')
    {
        echo 'Country Music Is What The Elderly Listen To!';
    }


    if ($_POST['agegroup'] == 'Elderly' && $_POST['music'] == 'Pop')
    {
        echo 'Pop Music Is For Young Kids';
    }
    elseif ($_POST['agegroup'] == 'Elderly' && $_POST['music'] == 'Rock')
    {
        echo 'Adults Listen To Rock Music';
    }
    elseif ($_POST['agegroup'] == 'Elderly' && $_POST['music'] == 'Country')
    {
        echo 'Country Music Is Perfect For The Elderly';
    }
}
?>

</body>

音乐调查
请输入您的名字

请输入您的年龄组 青年(12-21)
成人(22-50)
老年人(51-死亡)

请输入您喜爱的音乐风格 流行音乐
国家
摇滚乐



下面是PHP部分: 谢谢你接受调查
我有一个if语句,我需要返回,因为两个广播组都已选中,文本框已填写,否则,发送错误消息,由于某些原因,它不起作用,有人能帮我找出我缺少的内容吗?“年龄组”和“音乐”是广播组,“名字”是文本框

<?php

if(isset($_POST['agegroup']) && isset($_POST['music'])
&&!($_POST['firstname'] == NULL))
{
?>
<h1>Thank For Taking the Survey <?php echo $_POST['firstname'] ?> </h1>
else
{
echo "<h1>Please return to the form and fill out completely</h1>";
}

谢谢你接受调查
其他的
{
echo“请返回表格并完整填写”;
}

我认为您需要将前两个条件包含在一个()中,然后分别继续第三个条件。这样地。如果((cond1&&cond2)&&(cond3))

如果您有一个命名的提交按钮,则可以使用以下按钮:

if(isset($_POST['submit']) 
   && isset($_POST['agegroup']) 
   && isset($_POST['music']) 
   && !empty($_POST['firstname']))
它还将检查是否单击了提交按钮

或者您可以使用:

if(isset($_POST['agegroup']) 
   && isset($_POST['music']) 
   && !empty($_POST['firstname']))
这两种方法在测试以下内容时都起到了作用,并且作为一个示例,您可以对其进行修改,以包括表单其余部分使用的变量:

<form method="post" action="">

Age group: <input type="radio" name="agegroup" value="agegroup1">
<input type="radio" name="agegroup" value="agegroup2">

<br>
Music: <input type="radio" name="music" value="music1">
<input type="radio" name="music" value="music2">
<br>
First name <input name="firstname" type="text">
<br>
<input name="submit" type="submit" value="Submit">

</form>

<?php

// if(isset($_POST['agegroup']) && isset($_POST['music']) && !empty($_POST['firstname']))

$name = $_POST['firstname'];

if(isset($_POST['submit']) 
   && isset($_POST['agegroup']) 
   && isset($_POST['music']) 
   && !empty($_POST['firstname']))

    {
    echo "<h1>Thank For Taking the Survey " .$name. "</h1>";
    }

else
    {
    echo "<h1>Please return to the form and fill out completely</h1>";
    }

年龄组:

音乐:
名字

在打开
&&&后立即将错误报告添加到文件顶部!($\u POST['firstname']==NULL))
尝试删除
。或者
&($\u POST['firstname']!=NULL))
很难说您希望得到哪种情况。我现在就试试。错误报告没有任何作用,如果三个部分中的任何一个不存在,我需要php显示错误,因此,如果“firstname”留空,或者“agegroup”或“music”广播组中的任何一个留空,则“echo”将出现,并尝试切换“!”这不起作用,但是没有任何变化,当所有三个数据都被填充时,它会不断抛出“echo”,与我需要的正好相反尝试这个&($\u POST['firstname']!=null)我也这么做了,同样的结果:所有三个字段都被填充=echo我明天会尝试这个,非常感谢您的帮助,我会在我尝试后发布结果。弗雷德,这让我更接近了,我在下面的代码中有更多的“如果”语句,我想知道他们是否会抛出一些错误,b/c它仍然只会在所有3个区域(2个广播组和1个文本框)都被填充时抛出错误,Mike尝试将
&
更改为
|
——意思是
这个、
那个等等。而不是
这个、
那个相同的结果,我将发布我的整个html和PHP。我感觉程序里有什么东西把它搞乱了,b/c只是从逻辑的角度来看,这些修正是有意义的。@Mike重新加载我的答案,然后在编辑下看接近结尾的地方:(这个代码块)
<?php

if(isset($_POST['agegroup'])
    || isset($_POST['music']) 
    || !empty($_POST['firstname']))

    {
    echo "<h1>Please return to the form and fill out completely</h1>";
    }
<?php

if(!isset($_POST['agegroup'])
    || !isset($_POST['music']) 
    || empty($_POST['firstname']))

    {
    echo "<h1>Please return to the form and fill out completely</h1>";
    }