Codeigniter在一个页面中使用多个表单进行单独验证

时间:2017-04-16 09:00:01

标签: php forms codeigniter

我在一个页面中有两个表单。当我单击表单1的提交按钮时,两个表单上都会显示验证错误。如何在每个表单上显示单独的验证错误?

这是我的观点:

<?php echo form_open('user_signup/login',['class'=>'login-form','id'=>'submit_form']); 
        echo validation_errors();?>
            <h3 class="form-title font-green">Sign In</h3>
            <div class="alert alert-danger display-hide">
                <button class="close" data-close="alert"></button>
                <span> Enter any username and password. </span>
            </div>
            <div class="form-group">
                <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
                <label class="control-label visible-ie8 visible-ie9">Email</label>
  <?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
                 </div>
            <div class="form-group">
                <label class="control-label visible-ie8 visible-ie9">Password</label>
                <?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
                 </div>
            <div class="form-actions">
            <?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>


                <a href="javascript:;" id="forget-password" class="forget-password">Forgot Password?</a>
            </div>
            <div class="login-options">
                <h4>Or login with</h4>
                <ul class="social-icons">
                    <li>
                        <a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
                    </li>
                    <li>
                        <a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
                    </li>
                    <li>
                        <a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
                    </li>
                    <li>
                        <a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
                    </li>
                </ul>
            </div>
            <div class="create-account">
                <p>
                    <a href="javascript:;" id="register-btn" class="uppercase">Create an account</a>
                </p>
            </div>
        </form>
        <!-- END LOGIN FORM -->
        <!-- BEGIN FORGOT PASSWORD FORM -->

        <form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
            <h3 class="font-green">Forget Password ?</h3>
            <p> Enter your e-mail address below to reset your password. </p>
            <div class="form-group">
                <input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
            <div class="form-actions">
                <button type="button" id="back-btn" class="btn green btn-outline">Back</button>
                <button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
            </div>
        </form>
        <!-- END FORGOT PASSWORD FORM -->
        <!-- BEGIN REGISTRATION FORM -->
        <?php echo form_open('user_signup/login',['class'=>'register-form','id'=>'register_form']); 
        ?>

            <h3 class="font-green">Sign Up</h3>
            <p class="hint"> Enter your personal details below: </p>
            <div class="form-group">
                <label class="control-label visible-ie8 visible-ie9">Roll NO</label>
                <?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
                 </div>
            <div class="form-group">
                <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
                <label class="control-label visible-ie8 visible-ie9">Email</label>
                 <?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
                </div>

            <div class="form-actions">
                <button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
                <?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>

这是我的控制者:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_signup extends CI_Controller {
public function index()
{
    $this->load->view('User/signup');
}

    public function login()
{
     if ($this->input->post('submit_form')) {
  $rules['email1'] = 'required';
  $rules['pass'] = 'required';
  $this->form_validation->set_rules($rules);
}
else if ($this->input->post('register_form')) {
  $rules['rollno'] = 'required';
  $rules['email2'] = 'required';

  $this->validation->set_rules($rules);
}

if (!$this->form_validation->run()) {
  $this->load->view('User/signup');
}
else {
  if ($this->input->post('submit_form'))
      echo 'Form 1 posted !';
  else if ($this->input->post('register_form'))
      echo 'Form 2 posted !';
}
}
}

2 个答案:

答案 0 :(得分:0)

由Jquery提交。例如,在您的视图中

<form id="form1" action="" method="post">
  <label>Username</label>
  <input type="text" name="username">
  <label>Password</label>
  <input type="password" name="password">
  <input type="hidden" name="form_type" value="login">
  <button type="submit" onClick="login()">Login</button>
</form>
<form id="form2" action="" method="post">
  <label>Username</label>
  <input type="text" name="username">
  <label>Password</label>
  <input type="password" name="password">
  <label>Email</label>
  <input type="email" name="email">
  <input type="hidden" name="form_type" value="register">
  <button type="submit" onClick="register()">Login</button>
</form>

Jquery脚本

<script>
  function login()
  {
     $('#form1').submit();
  }
  function register()
  {
     $('#form2').submit();
  }
</script>

现在在您的控制器中

 if($_POST)
 {
    if($_POST['form_type']=='login')
    {
       // Validate Login
    }
    elseif($_POST['form_type']=='register')
    {
      // validate register
    }
 }

答案 1 :(得分:0)

您需要关注两件事。首先以HTML格式传递操作。其次,在控制器的操作中相应地获取这些参数和代码。

<?php echo form_open('user_signup/login/form-1',['class'=>'login-form','id'=>'submit_form']);
echo validation_errors();?>
<h3 class="form-title font-green">Sign In</h3>
<div class="alert alert-danger display-hide">
    <button class="close" data-close="alert"></button>
    <span> Enter any username and password. </span>
</div>
<div class="form-group">
    <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
    <label class="control-label visible-ie8 visible-ie9">Email</label>
    <?php echo form_input(['name'=>'email1','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email1')]); ?>
</div>
<div class="form-group">
    <label class="control-label visible-ie8 visible-ie9">Password</label>
    <?php echo form_input(['name'=>'pass','type'=>'password','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Password']); ?>
</div>
<div class="form-actions">
    <?php echo form_submit('submit', 'Submit',"class='btn green uppercase'" ); ?>


    <a href="javascript:;" id="forget-password" class="forget-password">Forgot Password?</a>
</div>
<div class="login-options">
    <h4>Or login with</h4>
    <ul class="social-icons">
        <li>
            <a class="social-icon-color facebook" data-original-title="facebook" href="javascript:;"></a>
        </li>
        <li>
            <a class="social-icon-color twitter" data-original-title="Twitter" href="javascript:;"></a>
        </li>
        <li>
            <a class="social-icon-color googleplus" data-original-title="Goole Plus" href="javascript:;"></a>
        </li>
        <li>
            <a class="social-icon-color linkedin" data-original-title="Linkedin" href="javascript:;"></a>
        </li>
    </ul>
</div>
<div class="create-account">
    <p>
        <a href="javascript:;" id="register-btn" class="uppercase">Create an account</a>
    </p>
</div>
</form>
<!-- END LOGIN FORM -->
<!-- BEGIN FORGOT PASSWORD FORM -->

<form class="forget-form" action="http://keenthemes.com/preview/metronic/theme/admin_2/index.html" method="post">
    <h3 class="font-green">Forget Password ?</h3>
    <p> Enter your e-mail address below to reset your password. </p>
    <div class="form-group">
        <input class="form-control placeholder-no-fix" type="text" autocomplete="off" placeholder="Email" name="email" /> </div>
    <div class="form-actions">
        <button type="button" id="back-btn" class="btn green btn-outline">Back</button>
        <button type="submit" class="btn btn-success uppercase pull-right">Submit</button>
    </div>
</form>
<!-- END FORGOT PASSWORD FORM -->
<!-- BEGIN REGISTRATION FORM -->
<?php echo form_open('user_signup/login/form-2',['class'=>'register-form','id'=>'register_form']);
?>

<h3 class="font-green">Sign Up</h3>
<p class="hint"> Enter your personal details below: </p>
<div class="form-group">
    <label class="control-label visible-ie8 visible-ie9">Roll NO</label>
    <?php echo form_input(['name'=>'rollno','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Rollno','value'=>set_value('rollno')]); ?>
</div>
<div class="form-group">
    <!--ie8, ie9 does not support html5 placeholder, so we just show field title for that-->
    <label class="control-label visible-ie8 visible-ie9">Email</label>
    <?php echo form_input(['name'=>'email2','type'=>'text','style'=>'text-transform: capitalize;','class'=>'form-control form-control-solid placeholder-no-fix','autocomplete'=>'off','placeholder'=>'Email','value'=>set_value('email2')]); ?>
</div>

<div class="form-actions">
    <button type="button" id="register-back-btn" class="btn green btn-outline">Back</button>
            <?php echo form_submit('register', 'register',"class='btn btn-success uppercase pull-right'" ); ?>

注意:检查表单操作

这是您更新的控制器:

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class User_signup extends CI_Controller {
public function index()
{
    $this->load->view('User/signup');
}

public function login()
{
    // Checking passed 3rd parameter in URI string.
    $submitted_form = $this->uri->segment(3)?$this->uri->segment(3):false;
    if ($submitted_form == 'form-1') {
        $rules['email1'] = 'required';
        $rules['pass'] = 'required';
        $this->form_validation->set_rules($rules);
    }
    if ($submitted_form == 'form-2') {
        $rules['rollno'] = 'required';
        $rules['email2'] = 'required';
        $this->validation->set_rules($rules);
    }

    if ($this->form_validation->run()) {
        if ($submitted_form == 'form-1')
            echo 'Form 1 posted !';
        else if ($submitted_form == 'form-1')
            echo 'Form 2 posted !';
    }
    $this->load->view('User/signup');
}
}

如果您有任何疑惑,请告诉我。