PHP if(isset) 无法正常使用 2 个无线电组和 1 个文本名称
PHP if(isset) not working properly with 2 radio groups and 1 textname
我再次遇到的问题是,当未选择文本字段或两个单选组中的任何一个时,它不会抛出 'error' 消息,而是仅在所有 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 语句,我需要 return 因为两个无线电组都已被选中并且文本框已被填写否则,传递错误消息并且由于某种原因它不起作用,可以有人帮我弄清楚我错过了什么吗? 'agegroup' 和 'music' 是单选框,'firstname' 是文本框。
<?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>";
}
我认为您需要将前两个条件包含在一个 () 中,然后分别继续第三个条件。像这样。如果((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>";
}
编辑:(此代码块)
<?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>";
}
我再次遇到的问题是,当未选择文本字段或两个单选组中的任何一个时,它不会抛出 'error' 消息,而是仅在所有 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 语句,我需要 return 因为两个无线电组都已被选中并且文本框已被填写否则,传递错误消息并且由于某种原因它不起作用,可以有人帮我弄清楚我错过了什么吗? 'agegroup' 和 'music' 是单选框,'firstname' 是文本框。
<?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>";
}
我认为您需要将前两个条件包含在一个 () 中,然后分别继续第三个条件。像这样。如果((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>";
}
编辑:(此代码块)
<?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>";
}