如何仅在成功注册时重置表单,但在提交失败时保留内容

时间:2015-09-16 05:12:38

标签: php html css forms codeigniter

<h3 class="text-upper background-blue1 top-margin-reg"> Register now</h3>
<h5 class="text-grey padding-bottom-20 padding-top-10">Don't have an Account?</h5>
<form action="<?=site_url('home/register')?>" method="post" class="border-left ">
    <?php if(isset($message)) '<span class="text-success txt-upper">'. $message . '</span>';?>
    <table class="table " style="margin-left: 11%;">
        <tr class="col-md-5">
            <td class="login-textfield">
                <input type="text" id="firstname" class="background-grey" placeholder="First name" name="firstname" value="<?php echo set_value('firstname')?>" required id="firstname" />
                <?php echo form_error( 'firstname'); ?>
            </td>
        </tr>
        <tr class="col-md-5">
            <td class="login-textfield">
                <?php echo form_input( 'lastname',set_value( 'lastname'), 'placeholder="Last name", class="background-grey" id="lastname" required id="lastname"')?>
                <?php echo form_error( 'lastname'); ?>
            </td>
        </tr>
        <tr class="col-md-5">
            <td class="login-textfield">
                <?php echo form_input( 'phone',set_value( 'phone'), ' placeholder="Phone Number", class="background-grey" id="phone" required id="phone"')?>
                <?php echo form_error( 'phone'); ?>
            </td>
        </tr>
        <tr>
            <td class="login-textfield">
                <?php echo form_input( 'username',set_value( 'username'), ' placeholder="Username", id="user_name" class="background-grey" required id="user_name"')?>
                <?php echo form_error( 'username'); ?>
            </td>
        </tr>
        <tr>
            <td class="login-textfield">
                <?php echo form_input( 'email',set_value( 'email'), ' placeholder="Email", class="background-grey" id="email" required id="email"')?>
                <?php echo form_error( 'email'); ?>
            </td>
        </tr>
        <tr>
            <td class="login-textfield">
                <input type="password" name="password" placeholder="Password" class="background-grey" id="password" required id="password" |matches[password_confirm]/>
                <?php echo form_error( 'password'); ?>
            </td>
        </tr>
        <tr>
            <td class="login-textfield">
                <input type="password" name="password_confirm" placeholder="Confirm Password" class="background-grey" id="password_confirm" required id="password_confirm" />
                <?php echo form_error( 'password_confirm'); ?>
            </td>
        </tr>
        <tr>
            <td class="login-textfield text-grey radio-pad">Gender
                <input type="radio" name="gender" value="male" checked/>male
                <input type="radio" name="gender" value="female" />female</td>
        </tr>
        <tr>
            <td class="login-textfield text-center padding-top-10">
                <input class="btn btn-primary btn-width-120 button-pos" type="submit" value="Register" name="register" id="form" style="width: 121%;" />
            </td>
        </tr>
    </table>
</form>

我很抱歉,但代码元素并没有为此工作。上面的文字是我的代码,我想在提交成功时重置表单,但在有验证错误时保留其中的内容。

控制器代码: -

public function register(){
    // Set up the form
    $rules = $this->user_m->rules_admin;
    $rules['password']['rules'] .= '|required';
    $this->form_validation->set_rules($rules);
    $this->form_validation->set_message('required', 'This field is required');
    $this->form_validation->set_error_delimiters('<small class="text-danger txt-upper">', '</small>');

    // Process the form
    if ($this->form_validation->run() == TRUE) 
    {
        if($this->user_m->add_user())
        {
            $this->data['message']= "You are registered successfully. Please login" ;


        }

        else
        {
            $this->data['message']= "Failed to register";
        }

    }
                $this->data['subview'] = 'home/login';
                $this->load->view('_layout_main_1', $this->data);
            //  $this->load->view('home/login', $this->data);
        }

2 个答案:

答案 0 :(得分:0)

当您遇到错误并在表单中显示值时,最好使用会话数组。但如果您有错误,只需重定向它,您的表单将在提交后重置。

答案 1 :(得分:0)

你的控制器代码需要改变如下(我猜,你已经知道了):

   // Process the form
if ($this->form_validation->run() == TRUE) 
{
    if($this->user_m->add_user())
    {
        $this->data['message']= "You are registered successfully. Please login" ;
     // Call login screen here, for example :
     redirect('login', 'location');

    }

    else
    {
        $this->data['message']= "Failed to register";
    }

}

更改表单代码,如下所示:(删除对set_value的调用)

            <td class="login-textfield">
            <?php echo form_input( 'phone','', ' placeholder="Phone Number", class="background-grey" id="phone" required id="phone"')?>
            <?php echo form_error( 'phone'); ?>

说明: 在form_validation失败时 - code_igniter尚未退出表单,因此在不使用set_value的情况下仍可使用在字段中输入的值。

成功注册后,当您转移到新表单时,您并不在意 - 登录表单。

尝试一下,让我知道它是否有效。