javascript - 事后检查后保留所选值

标签 javascript php html forms

我有一个队列。其中 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 留空时。我收到一条消息:请填写所有问题。

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

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

最佳答案

尝试下面的代码

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

关于javascript - 事后检查后保留所选值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31044697/

相关文章:

javascript - 如何在点击事件上创建可编辑输入

javascript - 更新d3数据到子节点

javascript - 设置 onMouseDown 的最短执行时间

javascript - 避免 document.write

javascript - 将 JS 变量传递给 HTML 文件

javascript - 不使用 QueryString 传递参数

php - Codeigniter csrf 保护,无需表单验证

php - 更改mysql中的文本编码

php - 使用 Javascript 为输入字段数组动态创建唯一 ID

css - 浏览器双重关闭我的标签?标记无效?