【问题标题】:Keep selected value after post check检查后保留所选值
【发布时间】:2015-09-11 17:26:03
【问题描述】:

我有一个队列。其中 1 个页面如下所示:

<form method="POST">
<input type="hidden" value="true" id="x" name="x">
    <table>
        <b>How relevant where the topics for your current and/or future business?</b>
        <hr />
        <tr>    
            <input type="hidden" value="1" name="question1">
            <td><input type="radio" name="answer1" value="1">1</td>
            <td><input type="radio" name="answer1" value="2">2</td>
            <td><input type="radio" name="answer1" value="3">3</td>
            <td><input type="radio" name="answer1" value="4">4</td>
            <td><input type="radio" name="answer1" value="5">5</td>
            <td><input type="radio" name="answer1" value="6">6</td>
            <td><input type="radio" name="answer1" value="7">7</td>
            <td><input type="radio" name="answer1" value="8">8</td>
            <td><input type="radio" name="answer1" value="9">9</td>
            <td><input type="radio" name="answer1" value="10">10</td>
            <td>
                <textarea rows="4" cols="50" name="comment1"></textarea>
            </td>
        </tr>
    </table>
        <br /><br />
    <table>
        <b>How did you value the networking opportunity?</b>
        <hr />
        <tr>
            <input type="hidden" value="2" name="question2">
            <td><input type="radio" name="answer2" value="1">1</td>
            <td><input type="radio" name="answer2" value="2">2</td>
            <td><input type="radio" name="answer2" value="3">3</td>
            <td><input type="radio" name="answer2" value="4">4</td>
            <td><input type="radio" name="answer2" value="5">5</td>
            <td><input type="radio" name="answer2" value="6">6</td>
            <td><input type="radio" name="answer2" value="7">7</td>
            <td><input type="radio" name="answer2" value="8">8</td>
            <td><input type="radio" name="answer2" value="9">9</td>
            <td><input type="radio" name="answer2" value="10">10</td>
            <td>
                <textarea rows="4" cols="50" name="comment2"></textarea>
            </td>
        </tr>
    </table>
    <input id="enquete_next" type="submit" name="Add" value="Next">
<?php 
//If the form gets submitted, check if everything is okay.
if(isset($_POST['x'])){
$validcomment = false;
        //validate if the answers are not empty. if they are empty go the the else statement.
        if(!empty($_POST['answer1'])){
            if(!empty($_POST['answer2'])){
                $validcomment = true;
            }else{
                echo "Please fill in all the questions!" . "<br>";
            }
        }else{
                echo "Please fill in all the questions!" . "<br>";
        }
        //If the form is filled in, and checked. Then do this!
        if($validcomment){
            insert_page1();
        }
}
?>
</form>

以下代码正在运行。因此,当我填写答案 1,但将答案 2 留空时。我收到一条消息:请填写所有问题。

但是,我希望表单保持其值。所以我只需要填写空答案而不是整个表格。

因为现在,当它检查时。表格变空了,我必须重新填写。

【问题讨论】:

  • 在谷歌上搜索核心 PHP 中的表单验证
  • 提交前先验证表单
  • 保存在 sessionStorage 或 php session 中

标签: javascript php html forms


【解决方案1】:

试试下面的代码

<form method="POST">
<input type="hidden" name="hiden_field" value="<?php $_POST['hiden_field'] ?>" id="x" name="x">
    <table>
        <b>How relevant where the topics for your current and/or future business?</b>
        <hr />
        <tr>    
            <input type="hidden" value="1" name="question1">
            <td><input type="radio" name="answer1" value="1" <?php if($_POST['answer1']==1){ echo "checked"; } ?>>1</td>
            <td><input type="radio" name="answer1" value="2" <?php if($_POST['answer1']==2){ echo "checked"; } ?>>2</td>
            <td><input type="radio" name="answer1" value="3" <?php if($_POST['answer1']==3){ echo "checked"; } ?>>3</td>
            <td><input type="radio" name="answer1" value="4" <?php if($_POST['answer1']==4){ echo "checked"; } ?>>4</td>
            <td><input type="radio" name="answer1" value="5" <?php if($_POST['answer1']==5){ echo "checked"; } ?>>5</td>
            <td><input type="radio" name="answer1" value="6" <?php if($_POST['answer1']==6){ echo "checked"; } ?>>6</td>
            <td><input type="radio" name="answer1" value="7" <?php if($_POST['answer1']==7){ echo "checked"; } ?>>7</td>
            <td><input type="radio" name="answer1" value="8" <?php if($_POST['answer1']==8){ echo "checked"; } ?>>8</td>
            <td><input type="radio" name="answer1" value="9" <?php if($_POST['answer1']==9){ echo "checked"; } ?>>9</td>
            <td><input type="radio" name="answer1" value="10" <?php if($_POST['answer1']==10){ echo "checked"; } ?>>10</td>
            <td>
                <textarea rows="4" cols="50" name="comment1"><?php if($_POST['comment1']){ echo $_POST['comment1']; } ?></textarea>
            </td>
        </tr>
    </table>
        <br /><br />
    <table>
        <b>How did you value the networking opportunity?</b>
        <hr />
        <tr>
            <input type="hidden" value="2" name="question2">
            <td><input type="radio" name="answer2" value="1" <?php if($_POST['answer2']==1){ echo "checked"; } ?>>1</td>
            <td><input type="radio" name="answer2" value="2" <?php if($_POST['answer2']==2){ echo "checked"; } ?>>2</td>
            <td><input type="radio" name="answer2" value="3" <?php if($_POST['answer2']==3){ echo "checked"; } ?>>3</td>
            <td><input type="radio" name="answer2" value="4" <?php if($_POST['answer2']==4){ echo "checked"; } ?>>4</td>
            <td><input type="radio" name="answer2" value="5" <?php if($_POST['answer2']==5){ echo "checked"; } ?>>5</td>
            <td><input type="radio" name="answer2" value="6" <?php if($_POST['answer2']==6){ echo "checked"; } ?>>6</td>
            <td><input type="radio" name="answer2" value="7" <?php if($_POST['answer2']==7){ echo "checked"; } ?>>7</td>
            <td><input type="radio" name="answer2" value="8" <?php if($_POST['answer2']==8){ echo "checked"; } ?>>8</td>
            <td><input type="radio" name="answer2" value="9" <?php if($_POST['answer2']==9){ echo "checked"; } ?>>9</td>
            <td><input type="radio" name="answer2" value="10" <?php if($_POST['answer2']==10){ echo "checked"; } ?>>10</td>
            <td>
                <textarea rows="4" cols="50" name="comment2"><?php if($_POST['comment2']){ echo $_POST['comment2']; } ?></textarea>
            </td>
        </tr>
    </table>
    <input id="enquete_next" type="submit" name="Add" value="Next">
<?php 
//If the form gets submitted, check if everything is okay.
if(isset($_POST['x'])){
$validcomment = false;
        //validate if the answers are not empty. if they are empty go the the else statement.
        if(!empty($_POST['answer1'])){
            if(!empty($_POST['answer2'])){
                $validcomment = true;
            }else{
                echo "Please fill in all the questions!" . "<br>";
            }
        }else{
                echo "Please fill in all the questions!" . "<br>";
        }
        //If the form is filled in, and checked. Then do this!
        if($validcomment){
            insert_page1();
        }
}
?>
</form>

【讨论】:

    【解决方案2】:
    <form method="POST">
    <input type="hidden" value="true" id="x" name="x">
        <b>How relevant where the topics for your current and/or future business?</b>
            <hr />
        <table>
    
            <tr>    
                <td><input type="hidden" value="1" name="question1"></td>
                <td><input type="radio" name="answer1" value="1">1</td>
                <td><input type="radio" name="answer1" value="2">2</td>
                <td><input type="radio" name="answer1" value="3">3</td>
                <td><input type="radio" name="answer1" value="4">4</td>
                <td><input type="radio" name="answer1" value="5">5</td>
                <td><input type="radio" name="answer1" value="6">6</td>
                <td><input type="radio" name="answer1" value="7">7</td>
                <td><input type="radio" name="answer1" value="8">8</td>
                <td><input type="radio" name="answer1" value="9">9</td>
                <td><input type="radio" name="answer1" value="10">10</td>
                <td>
                    <textarea rows="4" cols="50" name="comment1"><?php echo $_POST['comment1'];?></textarea>
                </td>
            </tr>
        </table>
            <br /><br />
            <b>How did you value the networking opportunity?</b>
            <hr />
        <table>
    
            <tr>
                <td><input type="hidden" value="2" name="question2"></td>
                <td><input type="radio" name="answer2" value="1">1</td>
                <td><input type="radio" name="answer2" value="2">2</td>
                <td><input type="radio" name="answer2" value="3">3</td>
                <td><input type="radio" name="answer2" value="4">4</td>
                <td><input type="radio" name="answer2" value="5">5</td>
                <td><input type="radio" name="answer2" value="6">6</td>
                <td><input type="radio" name="answer2" value="7">7</td>
                <td><input type="radio" name="answer2" value="8">8</td>
                <td><input type="radio" name="answer2" value="9">9</td>
                <td><input type="radio" name="answer2" value="10">10</td>
                <td>
                    <textarea rows="4" cols="50" name="comment2"><?php echo $_POST['comment2'];?></textarea>
                </td>
            </tr>
        </table>
        <input id="enquete_next" type="submit" name="Add" value="Next">
    <?php 
    //If the form gets submitted, check if everything is okay.
    if(isset($_POST['x'])){
    $validcomment = false;
            //validate if the answers are not empty. if they are empty go the the else statement.
            if(!empty($_POST['answer1'])){
                if(!empty($_POST['answer2'])){
                    $validcomment = true;
                }else{
                    echo "Please fill in all the questions!" . "<br>";
                }
            }else{
                    echo "Please fill in all the questions!" . "<br>";
            }
            //If the form is filled in, and checked. Then do this!
            if($validcomment){
                insert_page1();
            }
    }
    ?>
    </form>
    

    我可以修复文本区域,但不能修复单选按钮!!!
    Keep radio button selected after a form submit
    单选按钮!!

    【讨论】:

      【解决方案3】:

      您可以将数据存储在会话中。

      session_start();
      $_SESSION['data'] = $_POST['data'];
      

      然后在表格中你可以这样显示:

      session_start();
      <input type="text" name="data" value="<?php echo $_SESSION['data'];?>"/>
      

      关于会话变量的更多信息:Session variables - PHP.net

      而且,正如@Swaraj Giri 所提到的。您应该始终清理用户输入数据。这是 StackOverflow 上的一个问题,它对如何解决这个问题有一个很好的解释:What's the best method for sanitizing user input with PHP?

      【讨论】:

      • 请添加关于清理用户输入的注释。
      • 感谢您的意见@SwarajGiri
      猜你喜欢
      • 2015-02-25
      • 2011-01-30
      • 2011-12-26
      • 1970-01-01
      • 1970-01-01
      • 2020-04-10
      • 1970-01-01
      相关资源
      最近更新 更多