简体   繁体   English

标签选择 - 选择的选项

[英]Tag Select - selected option

I have a simple form with a select tag, I would like that after clicking on submit, what was marked was remembered.我有一个带有选择标签的简单表单,我希望在点击提交后,被标记的内容被记住。

Could you help me??你可以帮帮我吗?? How to correct the code below?如何更正下面的代码? I'm sure that I can't find it correctly on the Internet so I'm asking you for help.我确定我无法在 Internet 上正确找到它,所以我向您寻求帮助。

<form action="select.php" method="post">
    <select name="select">
        <option value="test1">test1</option>
        <option value="test2">test2</option>
        <option value="test3">test3</option>
        <option value="test4">test4</option>
    </select>
    <input type="submit" name="submit" value="Send">
</form>

Even though your question is not clear enough, as your question is not that straight forward.即使你的问题不够清楚,因为你的问题不是那么直截了当。

So I am going to give an answer based on an assumption :).所以我将根据假设给出答案:)。

  1. I am assuming you want to achieve this with php only.我假设您只想使用 php 来实现这一点。
  2. I am assuming that you are using POST request我假设您正在使用POST请求

  3. I am assuming that your form action='select.php' is the name of the page where your above code reside.我假设您的form action='select.php'是上述代码所在页面的名称。

Submitting the form to the same page.将表单提交到同一页面。

<!DOCTYPE html>
<html>
    <head>
        <title>This is the select page</title>
    </head>
<body>
    <?php 

        $select = '';

        if( isset($_POST['select']) ){
            $select = $_POST['select'];
        }

    ?>

    <form action="select.php" method="post">
        <select name="select">
            <option value="test1" <?php if($select == 'test1'): ?> selected <?php endif; ?>>test1</option>
            <option value="test2" <?php if($select == 'test2'): ?> selected <?php endif; ?>>test2</option>
            <option value="test3" <?php if($select == 'test3'): ?> selected <?php endif; ?>>test3</option>
            <option value="test4" <?php if($select == 'test4'): ?> selected <?php endif; ?>>test4</option>
    </select>
            <input type="submit" name="submit" value="Send">
    </form>
</body>
</html>

Submitting the form to different page.将表单提交到不同的页面。

<!DOCTYPE html>
<html>
    <head>
        <title>This is the select page</title>
    </head>
<body>
    <form action="another-page.php" method="post">
        <select name="select">
            <option value="test1">test1</option>
            <option value="test2">test2</option>
            <option value="test3">test3</option>
            <option value="test4">test4</option>
    </select>
            <input type="submit" name="submit" value="Send">
    </form>
</body>
</html>

and in the another-page.php you will have it written like thisanother-page.php你会像这样写

<!DOCTYPE html>
<html>
    <head>
        <title>This is the another page</title>
    </head>
<body>
    <form>
        <select name="select2">
            <?php 
                $select = $_POST['select'];
                if( isset($select) ){
                    echo "<option value='{$select}'> {$select} </option>";
                }else{
                    echo "<option value='' selected disabled> Nothing was selected </option>";
                }
            ?>
        </select>
    </form>
</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM