doupo2241 2011-11-03 18:44
浏览 28
已采纳

PHP - 表单:保存表单提交中的条目/选择

I have a PHP form that has some drop down selections and text field entries. If the user selects the wrong item from the dropdown, when they submit the form, I have it so that it will show an error message to the user and force the browser to go back to the previous page. The problem is that the user has to re-enter all of the information.

How do I make the form save the data until the form submit is successful?

EDIT:

Form submit method is $_POST and the form is being submitted to another page.

This would have to be done with strictly PHP as Javascript/Jquery solutions can be script blocked by more secure users.

  • 写回答

4条回答 默认 最新

  • drwpbrv668670 2011-11-04 07:05
    关注

    Here you go. This will work, and is not dependent on Javascript:

    form.php //the form page

    <?php session_start(); ?>
    <form method="post" action="action.php">
        <input type="text" id="input1" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input1'] : '') ?>" />
        <input type="text" id="input2" value="<?php echo (isset($_SESSION['fields']) ? $_SESSION['fields']['input2'] : '') ?>" />   
    </form>
    

    action.php //the action page

    <?php
        session_start();
    
        //do your validation here. If validation fails:
    
        $_SESSION['fields']['input1'] = $_POST['input1'];
        $_SESSION['fields']['input2'] = $_POST['input2'];
    
        //redirect back to form.php
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应