[英]PHP form results to same page
I have a very simle form, i wish the user to choose whether to sort ascending or decending. 我有一个非常简单的形式,我希望用户选择是升序还是降序。 From the select form, I will use the answer to give the search results in required order.
从选择表单中,我将使用答案按要求的顺序提供搜索结果。 My problem is that the form is not giving a result to the page and both 'if' statements are satisfied.
我的问题是表单没有给页面提供结果,并且两个'if'语句都满足。 I am completely stumped.
我完全难过了。 Can any one shed light?
谁能摆脱光明? Thank you
谢谢
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
?>
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
<?php if($_POST["thesort"]=="Lowest"){ echo 'selected="selected"';}
{
echo "<p> choice is ASC </p>";
?>
Why double curly braces? 为什么双花括号? PHP will execute the second one in ever case.
PHP将在任何情况下执行第二个。
if($_POST["thesort"]=="Highest")
{ echo 'selected="selected"';}
{echo "<p> choice is DESC </p>";}
Your code modified: 您的代码已修改:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest">Lowest first</option>
<option value="Highest">Highest first</option>
</select>
</form>
<?php
if(isset($_POST["thesort"])){
echo "selection has been made";
}
if($_POST["thesort"]=="Highest"){
echo 'selected="selected"';
echo "<p> choice is DESC </p>";
}
if($_POST["thesort"]=="Lowest"){
echo 'selected="selected"';
echo "<p> choice is ASC </p>";
}
?>
This is a problem: 这是个问题:
<?php if($_POST["thesort"]=="Highest"){ echo 'selected="selected"';}
{
echo "<p> choice is DESC </p>";
}
?>
a) Those brackets aren't doing what I think you think they're doing. a)那些括号不符合我认为你认为他们正在做的事情。 In particular, the second set are irrelevant;
特别是,第二组是无关紧要的; that code will always execute
该代码将始终执行
b) Why are you echo'ing 'selected=..' here? b)为什么你在这里回应'选择= ...'? It's not in the context of an open
<option
tag. 它不在open
<option
标签的上下文中。 For example, you probably want something like: 例如,您可能需要以下内容:
echo '<option value="Highest';
if ($_POST["thesort"]=="Highest")
{
echo ' selected="selected"';
}
echo '">Highest first</option>';
<?php
$sort = 'Lowest'; // define the default
if (isset($_POST['thesort']) && $_POST['thesort'] == 'Highest') {
$sort = 'Highest';
}
?>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<label for="sort">Sort by:</label>
<select name="thesort">
<option value="Lowest"<?php if ($sort == 'Lowest') print(' selected="selected"'); ?>>Lowest first</option>
<option value="Highest"<?php if ($sort == 'Highest') print(' selected="selected"'); ?>>Highest first</option>
</select>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.