如何从视图中提交到控制器中的文件

时间:2018-06-15 16:20:11

标签: php codeigniter codeigniter-3

我是一名新生,我正在学习 Codeigniter 。我有一个表单视图,我想提交名称,电子邮件,sdt到add.php进行检查,但我无法连接到add.php.

这是我的表格

<!DOCTYPE html>
<html>
<body>
<h2>Form Dang Ky</h2>
<form name="submitbd" action="add.php/check" method="POST">
 Name<br>
  <input type="text" name="name" value="">
      <br>
   Email:<br>
    <input type="text" name="email" value="">
    <br>
     SDT<br>
    <input type="text" name="sdt" value="">
    <br>
    <br>
   <input type="submit" value="Submit">
   <br>
 </form> 
 </body>
 </html>

我尝试在控制器中调用文件add.php,但我得到**

  

404错误

控制器中的Add.php文件

   <?php
   class Add extends IC_Controller{
    $data = array(
        '$_name' => $this->input->post('name'),
        '$email' => $this->input->post('email'),
        '$sdt'=>$this ->input-> post('sdt')
        );
    public function _contruct(){
        parent::_contruct();
    }
    public function  check(){
        $this->load->database();
        $a=$this->db->query("select email from info where 
    email=.$array($email)");
        if($a==""){
            echo "ok";
        }
        echo "not ok";           
    }
 }
 ?>

1 个答案:

答案 0 :(得分:0)

希望这会对您有所帮助

您的 form标记应如下所示:

<form name="submitbd" action="<?=site_url('add/check');?>" method="POST">
..........
</form> 

你的控制器 Add.php应该是这样的:

class Add extends CI_Controller
{
    public function _contruct(){
       parent::_contruct();
       $this->load->database();
       $this->load->helper('url');
    }
    public function  check()
    {
       $_name = $this->input->post('name');
       $email = $this->input->post('email');
       $sdt   = $this->input->post('sdt');
       $sql   = "select email from info where email='{$email}'";
       $query = $this->db->query($sql);
       if($query->num_rows() > 0)
       {
          echo "ok";
       }
       else
       {
          echo "not ok";           
       }
    }
}

了解更多信息:https://www.codeigniter.com/user_guide/general/index.html