错误条件下的CodeIgniter验证问题?

时间:2015-09-01 19:14:35

标签: php codeigniter

我正在使用CodeIgniter表单验证库。

示例视图:

<html>
    <head>
        <title>My Form</title>
    </head>

    <body>
        <?php echo validation_errors(); ?>

        <?php echo form_open('form'); ?>

            <h5>Username</h5>
            <input type="text" name="username" value="" size="50" />

            <h5>Password</h5>
            <input type="text" name="password" value="" size="50" />

            <h5>Password Confirm</h5>
            <input type="text" name="passconf" value="" size="50" />

            <h5>Email Address</h5>
            <input type="text" name="email" value="" size="50" />

            <div><input type="submit" value="Submit" /></div>
        </form>
    </body>
</html>

控制器

<?php
    class Form extends CI_Controller {

        function index()
        {
            $this->load->helper(array('form', 'url'));

            $this->load->library('form_validation');

            $this->form_validation->set_rules('username', 'Username', 'required');
            $this->form_validation->set_rules('password', 'Password', 'required');
            $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
            $this->form_validation->set_rules('email', 'Email', 'required');

            if ($this->form_validation->run() == FALSE)
            {
                $this->load->view('myform');
            }
            else
            {
                $this->load->view('formsuccess');
            }
        }
    }
?>

在这种情况下,当我填写用户名,密码,确认密码和电子邮件留空时,在提交表单后,收到错误的电子邮件要求。这很好,但在这种情况下我的用户名,密码,确认密码字段也丢失了数据,所以在发生任何错误时请帮助我。

不应该清除其他输入字段......

5 个答案:

答案 0 :(得分:0)

最后我建立了解决方案:

我们必须在value参数中使用set_value(),如下所示:

<?php echo set_value('username'); ?>

答案 1 :(得分:0)

最好的方法是通过Ajax帖子使用它。不要混淆是否。

答案 2 :(得分:0)

<input type="text" name="username" value=<?php echo $this->input->post("username");?> size="50" />

答案 3 :(得分:0)

使用以下代码替换您的表单代码:

<form>
    <h5>Username</h5>
    <input type = "text" name = "username" value = "<?php if (validation_errors()) { echo set_value('username'); } ?>" size = "50" />

    <h5>Password</h5>
    <input type = "text" name = "password" value = "<?php if (validation_errors()) { echo set_value('password'); } ?>" size = "50" />

    <h5>Password Confirm</h5>
    <input type = "text" name = "passconf" value = "<?php if (validation_errors()) { echo set_value('passconf'); } ?>" size = "50" />

    <h5>Email Address</h5>
    <input type = "text" name = "email" value = "<?php if (validation_errors()) {   echo set_value('email'); } ?>" size = "50" />

    <div><input type = "submit" value = "Submit" /></div>
</form>

答案 4 :(得分:0)

使用:

<html>
    <head>
        <title>My Form</title>
    </head>

    <body>
        <?php echo validation_errors(); ?>

        <?php echo form_open('form'); ?>

        <h5>Username</h5>
        <input type="text" name="username" value="<?php echo set_value('username');?>" size="50" />

        <h5>Password</h5>
        <input type="text" name="password" value="<?php echo set_value('password');?>" size="50" />

        <h5>Password Confirm</h5>
        <input type="text" name="passconf" value="<?php echo set_value('passconf');?>" size="50" />

        <h5>Email Address</h5>
        <input type="text" name="email" value="<?php echo set_value('email');?>" size="50" />

        <div><input type="submit" value="Submit" /></div>

        </form>
    </body>
</html>