簡體   English   中英

Codeigniter表單未提交

[英]Codeigniter form not submitting

我已經使用codeigniter表單驗證庫來驗證表單,以下是控制器中的邏輯,

public function addJob()
{
    $this->load->model('jobwall');
    if($query = $this->jobwall->getjobTitles())
    {
        $data['select_options'] = $query;
    }
    $this->load->helper('form');
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
    $this->form_validation->set_rules('job_title', 'Job title', 'required|trim');
    $this->form_validation->set_rules('job_location', 'Job location', 'required|trim');
    $this->form_validation->set_rules('job_postcode', 'Job postcode', 'required|trim|min_length[5]|max_length[8]');
    $this->form_validation->set_rules('basic_salary', 'Basic Salary', 'required|trim');
    $this->form_validation->set_rules('bonuses_available', 'bonuses_available', 'required|trim');
    $this->form_validation->set_rules('benefits', 'Benefits', 'trim');
    $this->form_validation->set_rules('key_skills', 'Key skills', 'trim');
    $this->form_validation->set_rules('retrain_position', 'Retrain position', 'required|trim');
    $this->form_validation->set_rules('job_summary', 'Job summary', 'required|trim|max_length[320]');
    $this->form_validation->set_rules('job_desc', 'Job description', 'required|trim');
    if($this->form_validation->run() == FALSE)
    {
        //die(var_dump($_POST));
        $this->template->build('admin/jobwall/addjob', $data);
    } else {
        $this->load->model('jobwall');
        if($query = $this->jobwall->saveJobToWall()){
            $this->jobwall->makeRelationship();
            redirect('dashboard/jobwall');
        }
    }
    $this->template->build('admin/jobwall/addjob', $data);
}

我嘗試過的表單返回的都是false,頁面只會重新加載表單(在false條件中指定),但是為了驗證它應該返回false。 有人可以在我的帖子數據中看到任何錯誤嗎?

 array(11) {
  ["job_title"]=>
  string(1) "1"
  ["job_location"]=>
  string(12) "Huddersfield"
  ["job_postcode"]=>
  string(7) "HD3 4AG"
  ["basic_salary"]=>
  string(19) "£15,000 - £20,000"
  ["bonus_available"]=>
  string(3) "yes"
  ["benefits"]=>
  string(33) "Great job, working on great sites"
  ["key_skills"]=>
  string(14) "PHP, CSS, HTML"
  ["retrain_position"]=>
  string(3) "yes"
  ["job_summary"]=>
  string(228) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s.o the leap into electronic typesetting, remaining essentially unchanged."
  ["job_desc"]=>
  string(743) "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. 

Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged."
  ["submit_job"]=>
  string(10) "Submit job"
}

您收到任何錯誤消息嗎? 以及為什么要使用$this->template->build('admin/jobwall/addjob', $data); 兩次?

我會嘗試增加return; 重定向功能之后,或刪除最后一行。

讓我們來看看:

// Step 1: check if everything is valid
// If not, it will build your template
if($this->form_validation->run() == FALSE)
{
    $this->template->build('admin/jobwall/addjob', $data);
}
// If yes...
else {
    $this->load->model('jobwall');
    if($query = $this->jobwall->saveJobToWall()){ // How do you handle your post here? Is it inside the model?
        $this->jobwall->makeRelationship();
        redirect('dashboard/jobwall');
        return; // to make sure that your code isn't running until the end of the fn
    }
}
// Step 2: You build the template for a 2nd time (if the validation did not pass), that could be a reason why your redirect isn't working, but i'm not sure
$this->template->build('admin/jobwall/addjob', $data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM