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


How to Reset form on successful registration only, yet keep content when when submission fails

<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);
        }

当出现错误并在表单中显示值时,最好使用会话数组。但是当你有一个错误,只需重定向它,你的表单将在提交后重置。

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

   // 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的情况下仍然可用。

在成功注册时,您不需要关心,因为您正在移动到一个新的表单-登录表单。