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

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.

我有一个PHP表单,其中包含一些下拉选项和文本字段条目。 如果用户从下拉列表中选择了错误的项目,那么当他们提交表单时,我会让它向用户显示错误消息并强制浏览器返回上一页。 问题是用户必须重新输入所有信息。 p>

如何在表单提交成功之前使表单保存数据? p> \ n

编辑: p>

表单提交方法是 $ _ POST code>,表单正在提交给另一个页面。 p>

这必须使用严格的PHP来完成,因为Javascript / Jquery解决方案可以被更安全的用户阻止。 p> div>

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

Is the form a POST or a GET? Either way, you have access to all the submitted fields in the PHP variables $_POST or $_GET. Within your HTML you can pass those values (if set), to the default value of each HTML input element. This way, if it is a first time, they will be blank, if there was an error, the values will repopulate.

If they're select values, you can do something like this:

<select name="my_select" id="my_select">
    <option value="123"<?php if($_REQUEST['my_select'] == 123) echo ' selected="selected"; ?>>123</option>
</select>

If you have regular text inputs, you can simply apply the $_REQUEST variable to the value attribute:

<input type="text" name="my_text" value="<?php echo $_REQUEST['my_text'] ?>" />

I suggest a preventing the page from navigating away from the submission until the data is verified. Enter jQuery :)

<script type="text/javascript" src="jquery-library.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        // Wait for the user to click on your button
        $('#submit_button').click(function(){
            // Check each form field for an appropriate value
            if ($('#form_field1').val() != 'something I expect')
            {
                alert('Wrong submission!');
                return false;
            }
            // Forward the user to some url location
            window.location = 'url';
            return false;
        });
    });
</script>