问题:设置默认值并记住PHP中选择框中的值 [英] Issue with setting the default value and remembering the values in select box in PHP

查看:78
本文介绍了问题:设置默认值并记住PHP中选择框中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form name =searchmethod =post> 
搜寻:< input type =textname =findvalue =<?php echo(isset($ _ POST ['find'])?$ _POST ['find']:'' );?> /> in
< Select NAME =field>
< Option VALUE =category1<?php echo(isset($ _ POST ['field'])&& $ _POST ['field'] ==='category1')? 'selected =selected':''; ?>>类别1< /选项>
< Option VALUE =category2<?php echo(isset($ _ POST ['field'])&& $ _POST ['field'] ==='category2')? 'selected =selected':''; ?>>类别2< /选项>
< /选择>
< input type =submitname =searchvalue =Search/>
< / form>
<?php
if(!empty($ _ POST)){
$ options = array('category1'=> array('1'=>'dog',' 2'=>'cat'),'category2'=>数组('1'=>'flower','2'=>'grass'));
$ input = trim($ _ POST ['find']);
$ category = $ _POST ['field'];
$ output = $ options [$ category] ​​[$ input];
echo $ output;
}
?>

问题:

我该如何制作category2是选择框而不是category1的默认值?我试过这个:

 < Option VALUE =category2<?php 
echo(isset($ _ POST ['field'])&& $ _POST ['field'] ==='category2')? 'selected =selected':'';
?> selected =selected> category2< / option>

但它破坏了我的功能。当我输入 1 并选择 category1 时,点击搜索后,选择框变为类别2 。这不是我想要的。我希望这个功能能够像这样执行:


  1. 记住用户为输入字段和选择框所设置的值。 $ b
  2. 为选择框设置默认值 category2

我怎么能做到这一点?

解决方案

试试默认会是cat2,选择后会得到结果

 < form method =post> 
< Select name =field>
< Option< php if(isset($ _ POST ['field'])&& $ _POST ['field'] ==category1)echo'selected =selected'; ?> VALUE =category1> category1< / option>
< Option< php if((isset($ _ POST ['field'])&& $ _POST ['field'] ==category2)或者空($ _ POST ['field' ]))echo'selected =selected;?> VALUE =category2> category2< / option>
< / Select>
< input type =submit/>
< / form>


<form name="search" method="post" >
 Seach for: <input type="text" name="find" value="<?php echo (isset($_POST['find']) ? $_POST['find'] : ''); ?>" /> in
 <Select NAME="field">
 <Option VALUE="category1" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category1') ? 'selected="selected"': ''; ?>>category1</option>
 <Option VALUE="category2" <?php echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"': ''; ?>>category2</option>
 </Select>
 <input type="submit" name="search" value="Search" />
 </form>
 <?php
    if(!empty($_POST)){
        $options = array('category1'=> array('1' => 'dog', '2' => 'cat'), 'category2' =>array('1'=>'flower', '2'=>'grass'));
        $input = trim($_POST['find']);
        $category = $_POST['field'];
        $output = $options[$category][$input];
        echo $output;
}
?>

Question:

How could I make category2 to be the default value for the select box instead of category1? I tried this:

<Option VALUE="category2" <?php
echo (isset($_POST['field']) && $_POST['field'] === 'category2') ? 'selected="selected"' : '';
?> selected = "selected">category2</option>

but it breaks my function. When I input 1 and select category1, after I clicked search, the select box changed to category2. That is not what I want. I want this function to perform like this:

  1. Remember the values that user make for input field and select box.
  2. Set the default value category2 for the select box.

How could I achieve this?

解决方案

try default will be cat2 and after selected will be result

  <form method="post"> 
<Select name="field">
 <Option <?php  if(isset($_POST['field']) && $_POST['field'] == "category1")  echo 'selected="selected"'; ?> VALUE="category1" >category1</option>
 <Option <?php if ((isset($_POST['field']) && $_POST['field'] == "category2") OR empty($_POST['field']))  echo 'selected="selected'; ?> VALUE="category2" >category2</option>
 </Select>
 <input type="submit" />
 </form>

这篇关于问题:设置默认值并记住PHP中选择框中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆