【发布时间】:2016-09-24 11:48:00
【问题描述】:
我正在学习 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();
}
}
}
【问题讨论】:
-
redirect()函数中不需要刷新参数,并确保dash/index是controller_name/action_name。试试这个:redirect('Dashboard/index');如果您的仪表板控制器中存在索引操作。 -
`/application/views/dash/Dashboard.php 你已经把它作为一个视图,应该在控制器中
-
感谢您的关注。它在正确的位置,我只是输入错误。立即修复。
-
if($this->email->send()) { return 1; } 其他 { 返回 0; } 添加这一行
-
从 $this->form_validation->run() === FALSE 更改为 $this->form_validation->run() == FALSE 和测试
标签: php forms codeigniter validation redirect