PHP 如果(isset) 不能正常工作,有 2 个无线电组和 1 个文本名


PHP if(isset) not working properly with 2 radio groups and 1 textname

我再次遇到的问题是,当文本字段或两个单选组中的任何一个未被选中时,它不会抛出"错误"消息,而是仅在填写所有 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>

我有一个 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>";
}

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

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>";
    }

编辑:(此代码块)

<?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>";
    }

我认为您需要将前两个条件括在一个 () 上,然后分别继续第三个条件。喜欢这个。If((cond1 && cond2) && (cond3))