首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP (Isset)与2个无线电组和1个文本名称不能正常工作

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

Stack Overflow用户
提问于 2015-02-04 00:52:14
回答 2查看 363关注 0票数 2

同样,我遇到的问题是,当文本字段或两个无线电组中的任何一个未被选中时,它不会抛出'error‘消息,而是只在三个字段都已被填充时抛出它们。下面是整个html & PHP页面:

代码语言:javascript
复制
<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>

我有一个如果声明,我需要返回,因为两个无线电组已被选中,文本框已填写,或其他,传递和错误信息,由于某种原因,它不工作,谁能帮助我找出我错过了什么?“年龄组”和“音乐”是广播组,“名字”是文本框。

代码语言:javascript
复制
<?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>";
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-02-04 05:00:56

如果有“命名提交”按钮,可以使用以下内容:

代码语言:javascript
复制
if(isset($_POST['submit']) 
   && isset($_POST['agegroup']) 
   && isset($_POST['music']) 
   && !empty($_POST['firstname']))

这也将检查提交按钮是否已被单击。

或者你可以使用:

代码语言:javascript
复制
if(isset($_POST['agegroup']) 
   && isset($_POST['music']) 
   && !empty($_POST['firstname']))

这两种方法在测试以下内容时都起作用,并且作为一个示例,您可以修改它以包含您正在为表单的其余部分使用的变量:

代码语言:javascript
复制
<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>";
    }

编辑:(此代码块)

代码语言:javascript
复制
<?php

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

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

这意味着,如果它们是设置的,而不是空的,那么就填写表格。

这需要是相反的:(如果没有设置或空.)

代码语言:javascript
复制
<?php

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

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

Stack Overflow用户

发布于 2015-02-04 01:08:10

我认为您需要将前两个条件放在一个()上,然后分别用第3条继续。就像这样。If((cond1 && cond2) && (cond3))

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28311610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档