【问题标题】:How do I set the combobox value on a page reset?如何在页面重置时设置组合框值?
【发布时间】:2012-06-30 00:21:31
【问题描述】:

当我因为 PHP 中的错误返回给用户时,我可以给文本框一个类似value = "<?php if(isset($_POST['abc'])){echo $_POST['abc'];} 的值。如何使用 <select> 组合框进行等效操作?

【问题讨论】:

  • 我不确定这不能在 php 中实现。我们可以使用 jquery cookie 代替 php 来解决这个问题

标签: php webforms


【解决方案1】:

这有点取决于refreshed 的含义。

如果您的意思是在没有任何发布的情况下对页面进行字面刷新/重新加载,您可以使用 javascript 在更改时存储该值,并在页面刷新/加载时加载该值。您的存储选项各不相同,从访问者计算机上的 cookie 和本地存储到使用 ajax 的服务器端存储。

如果您在稍后加载同一页面时谈论使用以前保存/发布的值,那就另当别论了,尽管 ajax 方法在那里也可以工作(尽管可能没有必要)。

编辑:根据您的编辑,您需要将selected="selected" 添加到之前选择的选项中。

因此,在构建您的选择选项的循环中(假设是一个循环),您可以执行以下操作:

foreach($options as $key => $value)    // the loop, if any...
{
  echo "<li value='{$key}'";
  if ($key == $_POST['def'])
  {
    echo ' selected="selected"';
  }
  echo ">{$value}</li>"
}

【讨论】:

    【解决方案2】:

    这是一个示例:

    <?php
        $cbo_sel = 'SELECTED="SELECTED"'; //to make an item selected, append this attribute to that <option> element
    
        $post_success = false; //becomes true only when the data submitted is processed successfully
    
        $name   = '';
        $email  = '';
        $grade  = '';
    
        $user_msg = ''; //holds the messages to the user
    
        if($_SERVER['REQUEST_METHOD']=='POST')
        {
            //assigning the values
            $name   = isset($_POST['name']) ? trim($_POST['name']) : '';
            $email  = isset($_POST['email']) ? trim($_POST['email']) : '';
            $grade  = isset($_POST['grade']) ? trim($_POST['grade']) : '';
    
            //do validation here
            if($name == '') // && any_regex_if_needed
                $user_msg = "Where is your name ?";
            else if($email == '')
                $user_msg = "How am I gonna contact you ?";
            else if($grade == '')
                $user_msg = "Oh! Don't hide your grade from me !";
            else
            {
                //everything is okay.. now do your things here... like inserting this to database or calculating or mailing, etc...
                //...
                //...
    
                $user_msg = 'Success !';
                $post_success = true; //success
            }
        }
    ?>
    <!DOCTYPE html>
    <html>
    <head><title>Testing..</title></head>
    <body>
    
        <div><?php echo $user_msg; ?></div>
    
        <form method="POST">
            Name: 
                <input type="text" name="name" value="<?php echo (!$post_success)?$name:''; ?>" /> <br />
            Email: 
                <input type="text" name="email" value="<?php echo (!$post_success)?$email:''; ?>" /> <br />
            Grade: 
                <select name="grade">
                    <option value="A" <?php echo ($grade=='A' && !$post_success)?$cbo_sel:''; ?>>A</option> <!-- if grade='A' and if the data submitted was not processed(because of failed validation), then display the value -->
                    <option value="B" <?php echo ($grade=='B' && !$post_success)?$cbo_sel:''; ?>>B</option>
                    <option value="C" <?php echo ($grade=='C' && !$post_success)?$cbo_sel:''; ?>>C</option>
                    <option value="D" <?php echo ($grade=='D' && !$post_success)?$cbo_sel:''; ?>>D</option>
                </select>
    
                <input type="submit" name="submit" value="Submit" />
        </form>
    </body>
    </html>
    

    在这里,我正在对选项进行硬编码。因此,如果下拉菜单包含多个项目,请使用循环并使用 if 语句检查选项。

    也许这可以改进。我欢迎所有建议。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-25
      • 2017-10-03
      • 1970-01-01
      • 1970-01-01
      • 2020-10-01
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多