无法验证表单数据或插入数据库(Codeigniter)

时间:2016-02-07 10:03:51

标签: php forms twitter-bootstrap codeigniter

这些天我正在使用Codeigniter框架。我正在尝试使用codeigniter的form_validation库来验证我的表单数据。问题是我无法让它发挥作用。我不知道问题出在哪里,没有显示错误。

以下是视图页面我在表单操作 核心并运行 postad >

<div class="container table-responsive" id="con1">



    <div class="row" id="h4_subad">
        <div class="col-md-4"><h4>Submit an Ad</h4></div>
    </div>

<form class="form-horizontal" role="form" method="get" action="/core/postad">
   <table id="form_table" class="table-responsive table table-striped" id="table_form">

       <td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>

       <tr>  <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
  <option>Electronics</option>
  <option>Mobiles & Accessories</option>
  <option>Computers & Accessories</option>
  <option>Kitchen Applicances</option>
  <option>Clothing</option>
               </select></td></tr>

       <tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>

       <tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
   </table>

</form>  
</div>

此视图的控制器

class Core extends CI_Controller {

    public function index() {


        $this->load->view('header1');
        $this->load->view('body');
        $this->load->view('footer');
    }

    public function product() {


        $this->load->view('header1');
        $this->load->view('product');
        $this->load->view('footer');
    }

    public function contact() {


        $this->load->view('header1');
        $this->load->view('contact');
        $this->load->view('footer');
    }

    public function aboutus() {

        $this->load->view('header1');
        $this->load->view('aboutus');
        $this->load->view('footer');
    }
      public function reg() {

        $this->load->view('header1');
        $this->load->view('reg');
        $this->load->view('footer');
        $this->input->get('username');

      }

       public function postad() {

        $this->load->view('header1');
        $this->load->view('postad');
        $this->load->view('footer');

        $this->load->model('insert_model');
   $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

//Validating Name Field
$this->form_validation->set_rules('txt_area', 'Text Area', 'required|min_length[5]|max_length[15]');



if ($this->form_validation->run() == FALSE) {
$this->load->view('postad');
}

else {
//Setting values for tabel columns
$data = array(
'description' => $this->input->post('txt_area'),
);
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
$this->load->view('insert_view', $data);
}
}
}

数据库连接模型

<?php
class insert_model extends CI_Model{
function __construct() {
parent::__construct();
}
function form_insert($data){

$this->db->insert('post_ad', $data);
}
}
?>

仅针对一个字段 txt_area

进行尝试

我不想使用codeigniter的表单助手,因为我正在使用bootstrap表单。

3 个答案:

答案 0 :(得分:0)

您需要在posting.php文件( HTML )中使用validation_errors();来显示您验证的显示错误消息。

<?php echo validation_errors(); ?>

Form Validation User Manual

答案 1 :(得分:0)

您应该在html中回复下面的validation_errors();。有关更多信息,您可以访问this  

<?php echo validation_errors(); ?>

    <div class="row" id="h4_subad">
        <div class="col-md-4"><h4>Submit an Ad</h4></div>
    </div>

<form class="form-horizontal" role="form" method="get" action="/core/postad">
   <table id="form_table" class="table-responsive table table-striped" id="table_form">

       <td>Ad Title*</td><td><input class="form-control" name="ad_title" id="ad_title" type="text" placeholder="Enter Title" id="ad_form"></td>

       <tr>  <td>Select Category*</td> <td><select name="ad_form" class="form-control" id="ad_form">
  <option>Electronics</option>
  <option>Mobiles & Accessories</option>
  <option>Computers & Accessories</option>
  <option>Kitchen Applicances</option>
  <option>Clothing</option>
               </select></td></tr>

       <tr><td>Ad Description*</td><td><textarea name="txt_area" id="txt_area" class="form-control" type="text" placeholder="Enter Description" ></textarea></td></tr>

       <tr><td><button type="button" class="btn btn-warning" onclick="core/postad">Submit Ad</button></td></tr>
   </table>

</form>  
</div>

答案 2 :(得分:0)

您提交表单的按钮不正确。当您尝试提交表单时,请务必将类型设置为提交。

<button type="submit">Submit form</button>

<submit>Submit form</submit>