成功完成表单验证后,CodeIgniter不会重定向

时间:2016-09-24 11:48:01

标签: php forms codeigniter validation redirect

我正在学习CodeIgniter,因为我不得不接管一个离开项目中期的团队成员的项目。总的来说,我取得了很好的进展,但这一个问题让我完全陷入困境。

就基本结构而言,我们有以下几点:

/application/controllers/Dashboard.php

function add_record() {
    $locations = $this->db->get('locations');
    $location['loc_data'] = $locations->result_array();

    $this->load->helper('form');
    $this->load->view('dash/add_record', $location);
}

function create_record() {
    $this->load->helper('form');
    $this->load->library('form_validation');

    $this->form_validation->set_rules('customer_first_name', NULL, 'required');
    $this->form_validation->set_rules('customer_last_name', NULL, 'required');
    $this->form_validation->set_rules('customer_email', NULL, 'required');
    $this->form_validation->set_rules('customer_phone', NULL, 'required');
    $this->form_validation->set_rules('customer_address', NULL, 'required');
    $this->form_validation->set_rules('customer_city', NULL, 'required');
    $this->form_validation->set_rules('customer_state', NULL, 'required');
    $this->form_validation->set_rules('customer_zip', NULL, 'required');
    $this->form_validation->set_rules('customer_message', , 'required');

    if ($this->form_validation->run() === FALSE) {
        $this->load->view('dash/add_record');
    }else{
        $user_id = $this->session->userdata('user_id');
        $data = array(
            'customer_first_name' => $this->input->post('customer_first_name'),
            'customer_last_name' => $this->input->post('customer_last_name'),
            'customer_email' => $this->input->post('customer_email'),
            'customer_phone' => $this->input->post('customer_phone'),
            'customer_address' => $this->input->post('customer_address'),
            'customer_city' => $this->input->post('customer_city'),
            'customer_state' => $this->input->post('customer_state'),
            'customer_zip' => $this->input->post('customer_zip'),
            'customer_message' => $this->input->post('customer_message'),
            'status' => 1,
            'user_id' => $user_id,
            'created_on' => time(),
        );

        $this->db->insert('customer_message', $data);
        $this->send_status($data['customer_email'],$data['status'], $etext='', $data);
        redirect('dash/index', 'refresh');
    }

}


/application/dash/add_record.php
<?php $this->load->view('header');?>
 <?php echo form_open_multipart("dashboard/create_record",'id="my-record" style="width:100%;border:0;padding:0;"');?>
          <label>Customer First Name
            <?php echo form_input('customer_first_name', set_value('customer_first_name'), 'placeholder="Customer First Name" required');?>
          </label>
          <label>Customer Last Name
            <?php echo form_input('customer_last_name', set_value('customer_last_name'), 'placeholder="Customer First Name" required');?>
          </label>
          <label>Customer Email
            <?php echo form_input('customer_email', set_value('customer_email'), 'placeholder="Customer Email" required');?>
          </label>
          <label>Customer Phone
            <?php echo form_input('customer_phone', set_value('customer_phone'), 'placeholder="Customer Phone Number" required');?>
          </label>
          <label>Customer Address
            <?php echo form_input('customer_address', set_value('customer_address'), 'placeholder="Customer Address" required');?>
          </label>
          <label>Customer City
            <?php echo form_input('customer_city', set_value('customer_city'), 'placeholder="Customer City" required');?>
          </label>
          <label>Customer State
            <?php echo form_input('customer_state', set_value('customer_state'), 'placeholder="Customer State" required');?>
          </label>
          <label>Customer Zip Code
            <?php echo form_input('customer_zip', set_value('customer_zip'), 'placeholder="Customer Zip" required');?>
          </label>
        <label>Message
          <?php echo form_textarea('customer_message', set_value('customer_message'), 'placeholder="Message" required');?>
        </label>
        <input type="file" name="userfile" size="20" multiple/>
        <?php echo form_submit('submit', 'Submit', 'id="submit-all" class="button"');?>
<?php echo form_close();?>
<?php $this->load->view('footer');?>

最终发生的是用户提交表单并进行验证。它正在捕获错误并强迫您更正它们,但是如果所有字段都检出,则它会在后台静默提交表单并将行添加到数据库中,同时保留表单(它甚至不会重新加载任何内容) ;没有闪烁)。

如果行为正常,则会转到仪表板索引(/dash/index.php)。

我已尝试评论所有set_rules()次来电,但仍会进行验证。我已经尝试评论if/else块并强制它提交数据并重定向,但它仍然执行验证并保留在表单上,​​看起来好像什么也没发生。

项目中的其他重定向工作正常;这是唯一一个有问题的人,比较代码确实没有任何结构差异。这是怎么回事?

====编辑====

根据请求,这是send_status函数:

/application/controllers/Dashboard.php

class Dashboard extends CI_Controller {

    // [...]

    public function send_status($to,$status, $etext='', $template)
    {
        // load email library
        $this->load->library('email');
        if($etext==''){
            if($status == 1){
                $id = 1;
            }
            if($status == 2){
                $id = 3;
            }
            if($status == 3){
                $id = 2;
            }
            $this->db->where('id', $id);
            $query = $this->db->get('email');
            $mail = $query->row();  
            $etext = $mail->template;
        }
        if($status != 0){

            foreach($template as $key => $value){
                $etext = str_replace('{'.$key.'}', $value, $etext);
            }

            $this->email
                ->from($mail->sender_email, 'Company Name')
                ->to($to)
                ->subject('Your Submission')
                ->message($etext)
                ->set_mailtype('html');

            // send email
            $this->email->send();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

正如您所描述的那样,正确插入数据意味着此行没有问题。

import numpy as np

mu,sigma,n = 0.,1.,1000

def normal(x,mu,sigma):
    return ( 2.*np.pi*sigma**2. )**-.5 * np.exp( -.5 * (x-mu)**2. / sigma**2. )

x = np.random.normal(mu,sigma,n) #generate random list of points from normal distribution
y = normal(x,mu,sigma) #evaluate the probability density at each point
x,y = x[np.argsort(y)],np.sort(y) #sort according to the probability density

改变
$this->db->insert('customer_message', $data);

$this->send_status($data['customer_email'],$data['status'], $etext='', $data);

更改后执行

$result =$this->send_status($data['customer_email'],$data['status'], $etext='', $data);
if($result){
    redirect('dash/index', 'refresh');
}

$this->email->send(); 

如果邮件功能出现问题,则不会重定向

答案 1 :(得分:0)

找出问题的原因。除了CodeIgniter的内置表单验证之外,之前的程序员已经安装了Parsley.js进行表单验证,然后继续劫持提交行为,向处理表单数据的页面发送AJAX调用,然后忽略了此后重定向。删除了Parsley.js代码,它现在正常运行。