无效表单提交后如何保留页面(CodeIgniter 3)

时间:2015-07-30 06:39:31

标签: javascript php jquery html codeigniter

如果持有我的表单的<div>标记具有“隐藏”类,并且只有在单击该页面上的“添加”按钮时才能看到,如果在提交的表单无效后我如何在该页面上保留。

当您点击该页面上的添加按钮时,这就是我的URI / URL。

http://localhost/CITESTING/index.php/gatepass#new-gatepass-form

其中#new-gatepass-form是您将看到表单的div。

当我尝试刷新该URL时,页面返回到默认div并隐藏表单div。

if ($this->form_validation->run() == true && $this->gatepass_model->addgatepass($adddata))
        { 
            //goes to the default view where form div hides
            //TRUE
            $this->load->view("gatepass");
        }
        else {
            //what should I put here?
        }

gatepass.php [controller]

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

class Gatepass extends CI_Controller 
{
    public function __construct() {
        parent:: __construct();

        $this->load->helper("url");
        $this->load->helper('form');
        $this->load->model("gatepass_model");
        $this->load->library("form_validation");
    }

    //create a new user
    public function index()
    {
        //$this->load->view('header');
        //$this->load->view('gatepass');

        $this->load->model("gatepass_model");
        $data['records']= $this->gatepass_model->gatepassList();


        $this->load->view('header');
        $this->load->view("gatepass", $data);
    }

     function add_form()
    { //CREATE
        $this->form_validation->set_rules('gatepassNo', 'Gate Pass No', 'required|strip_tags|trim|xss_clean');
        $this->form_validation->set_rules('serviceCardNo', 'Service Card No', 'required|strip_tags|trim|xss_clean');
        $this->form_validation->set_rules('spareParts', 'Spare Parts', 'required|strip_tags|trim|xss_clean');
        $this->form_validation->set_rules('serviceNo', 'Service NO', 'required|strip_tags|trim|xss_clean');
        $this->form_validation->set_rules('accessories', 'Accessories', 'required|strip_tags|trim|xss_clean');
        $this->form_validation->set_rules('dateClaimed', 'Date Claimed', 'callback_dateClaimed_check');


        if ($this->form_validation->run() == true)
        {
            $adddata = array(
                'gatepass_no'       => $this->input->post('gatepassNo'),
                'servicecard_no'    => $this->input->post('serviceCardNo'),
                'item_no'           => $this->input->post('spareParts'),
                'service_no'        => $this->input->post('serviceNo'),
                'accessories'       => $this->input->post('accessories'),
                'date_claimed'      => $this->input->post('dateClaimed'),

                );
        }
        if ($this->form_validation->run() == true && $this->gatepass_model->addgatepass($adddata))
        { 
            //check to see if we are creating the user
            //redirect them to checkout page
            redirect("gatepass");
        }
        else {


        }
    }

gatepass.php [查看]

<div id='gatepass-records' class="hide">

    //some records table and default window
</div>

<div id='new-gatepass-form' class="hide">

    <?php echo form_open('gatepass/add_form'); ?>

        <div class="form-horizontal">
            <div class="form-group">
                <label class="col-sm-4 control-label">Gate Pass No:</label>
                <div class="col-sm-8">
                    <?php echo form_error('gatepassNo'); ?>
                    <input type="text" name="gatepassNo" class="form-control" value="<?php echo set_value('gatepassNo');?>" />
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">Service Card No:</label>
                <div class="col-sm-8">
                    <?php echo form_error('serviceCardNo'); ?>
                    <input type="text" name="serviceCardNo" class="form-control" value="<?php echo set_value('serviceCardNo');?>">
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label">Spare Parts:</label>
                <div class="col-sm-8">
                    <input type="text" name="spareParts" class="form-control" value="<?php echo set_value('spareParts');?>">
               </div>
            </div>
            <div class="form-group">
                <div class="col-sm-6"></div>
                    <div class="col-sm-4">
                        <input type="submit" name="submit" class="btn btn-success btn-lg" value="Submit" />
                    </div>
                    <div class="col-sm-6"></div>
            </div>
        </div>
          <?php echo form_close(); ?>
</div>


<div id='gatepass-modify' class="hide">

    //still working on this
</div>

我在外部js文件上有这个脚本。

/********************* G A T E   P A S S   W I N D O W **********************/
    if(elem_id == 'gatepass-window')
    {   
        if(elem_id == '#view-gatepass-btn') {
            $('#gatepass-records').removeClass('hide');
        }
        if(elem_id == '#modify-gatepass-btn') {
            $('#gatepass-modify').removeClass('hide');
        }
        if(elem_id == '#new-gatepass-btn') {
            $('#gatepass-form').removeClass('hide');
        }records
        if(elem_id == '#delete-gatepass') {

        }
    } 
    else 
    {
        $('#gatepass-records').removeClass('hide');

         $('#modify-gatepass-btn').on('click', function(e) {
            $('#gatepass-records').removeClass('hide');
            $('#gatepass-form').addClass('hide');
        });

        $('#new-gatepass-btn').on('click', function(e) {
            $('#gatepass-form').removeClass('hide');
            $('#gatepass-records').addClass('hide');
        });
    }
/************ E N D   O F   G A T E   P A S S   W I N D O W ***************/

1 个答案:

答案 0 :(得分:1)

我已经看到了你的代码,需要按照下面的代码修改它。

  1. 在表单中放入一个包含表单哈希值的隐藏字段,然后将其与您的表单一起提交。获取后,在flashdata中设置错误信息,如

          $this->session->set_flashdata('error',"some field are required"); and  then use redirect on same tab or div by catching that hash value in hidden like bellow.
    
        $hash=$this->input->post("hashfieldname");  //hasfieldname is hidden field in that form
    
  2. 然后使用重定向

          $url='/gatpass#'.$hash;
          redirect($url);
    

    在这种情况下,您将能够显示单个消息,因为您需要重定向,因为您的表单操作与实际的URL不同。