duanduanxi9441 2015-06-25 08:10
浏览 24
已采纳

邮寄检查后保留选定的值

I have a enquete. 1 of the pages looks like this:

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

The following code is working. So when i fill in answer 1, but leave answer 2 empty. i get a message: Please fill in all the questions.

However, i would like the form to keep its values. so i only have to fill in the empty answer instead of the whole form.

Because right now, when it checks. The form gets empty and i have to fill it in all over again.

  • 写回答

3条回答 默认 最新

  • doushan6161 2015-06-25 08:38
    关注

    Try below code

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?