dongshi3361 2014-10-01 10:27
浏览 494
已采纳

提交表单后如何保持下拉菜单选择值?

I am creating a very simple form with a dropdown menu and a text filed. It shows the errors if the form has empty field during the submission.

I want to show the filled values after the form submission if there is any error. It works on text field as I expected. Ex: If fill the name and submit the form without filling the title dropdown menu, it showing the name I typed on the filed during the the error is appeared.

But how can I do that for dropdown menu also? Ex: If I select the title drop down menu and submit the form without filling name field, it should show the selected title dropdown value during the the error is appeared.

how can I do that? here is my code and it's a wordpress site:

<?PHP
$errors = array();
    if($_POST["submit"]) {
        $name_title = $_POST["name_title"];
        $sender = $_POST["sendername"];

        //Check the name title that it is selected or none.
                if($name_title === none){
                    //if selected is none, add error to $errors array.        
                    $errors['name_title'] = "Please select the title of your name!";
                }

        if(empty($sender)){
            //Blank string, add error to $errors array.        
            $errors['sendername'] = "Please enter your name!";
        }

        // sending form
        if(empty($errors)){
            $mail_sent = wp_mail( $to, $subject, $mailBody, $headers ); 
        }       

    }
    if ($mail_sent) {
?>

<h1 style="color: #007f00;">Request sent.</h1>

<?php 
} else {
?>

<form id="" name="" action="<?php echo get_permalink(); ?>" method="post">
    <div class="label-input-wrapper">
        <div class="form-label">Title</div>
        <div class="form-input">
            <select name="name_title" class="name-title-input">                 
                <option value="none" selected="selected">Select Title</option>
                <option value="Mr">Mr</option>
                <option value="Mrs">Mrs</option>
                <option value="Miss">Miss</option>
                <option value="4">Ms</option>                   
            </select>
            <div class="error-msg">
                <?php if(isset($errors['name_title'])) { echo '<span style="color: red">'.$errors['name_title'].'</span>'; } ?>
            </div>  
        </div>
    </div>  

    <div class="label-input-wrapper">
        <div class="form-label">Name</div>
        <div class="form-input">
            <input type="text" name="sendername" value="<?PHP if(!empty($errors)) { echo $sender;} ?>" />
            <div class="error-msg">
                <?php if(isset($errors['sendername'])) { echo '<span style="color: red">'.$errors['sendername'].'</span>'; } ?>
            </div>
        </div>
    </div>
    <input type="submit" value="Submit" name="submit">
</form>

<?php
}  
?>
  • 写回答

3条回答 默认 最新

  • dongzhimin2231 2014-10-01 10:42
    关注

    although the answer by @CKocer can also work but I like to use variables instead and using $_POST in HTML and also more readable

    In declare $name_title out side of if($_POST) and can do it like this <?php $name_title = ''; ?>

    For Drop Down code change like this

     <select name="name_title" class="name-title-input">                 
                    <option value="none" selected="selected">Select Title</option>
                    <option value="Mr" <?php if($name_title == 'Mr') { ?> selected <?php } ?>>Mr</option>
                    <option value="Mrs" <?php if($name_title == 'Mrs') { ?> selected <?php } ?>>Mrs</option>
                    <option value="Miss" <?php if($name_title == 'Miss') { ?> selected <?php } 
    ?>>Miss</option>
                  <option value="4" <?php if($name_title == 'Ms') { ?>selected <?php  } ?>>Ms</option>                   
                </select>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥66 如何制作支付宝扫码跳转到发红包界面
  • ¥15 pnpm 下载element-plus
  • ¥15 解决编写PyDracula时遇到的问题