Html 标记选择-选定选项

Html 标记选择-选定选项,html,forms,validation,select,tags,Html,Forms,Validation,Select,Tags,我有一个简单的表单和一个选择标签,我想在点击提交后,被标记的内容被记住 你能帮我吗??如何更正下面的代码? 我确信我在互联网上找不到它,所以我请求你的帮助 <form action="select.php" method="post"> <select name="select"> <option value="test1">test1</option> <option value="test2">

我有一个简单的表单和一个选择标签,我想在点击提交后,被标记的内容被记住

你能帮我吗??如何更正下面的代码? 我确信我在互联网上找不到它,所以我请求你的帮助

<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>

测试1
测试2
测试3
测试4

即使你的问题不够清楚,因为你的问题不是那么直截了当

所以我将根据一个假设给出一个答案:)

  • 我假设您只想用php实现这一点
  • 我假设您正在使用
    POST
    请求

  • 我假设您的
    表单action='select.php'
    是上述代码所在页面的名称

  • 将表单提交到同一页面。

    <!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>
    
    <!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>
    

    可以进行Ajax调用以防止页面加载。看到这个我很抱歉,我写得这么不清楚,我希望我的下一篇文章会更清楚。谢谢你的全面回答。你是最棒的!:)不客气。很高兴我能帮忙;如果它符合您的需要,请将其标记为已接受的答案。
    <!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>