簡體   English   中英

無法在Codeigniter中查看表單頁面

[英]Unable to view form page in Codeigniter

當我嘗試打開時,無法在Codeigniter中查看表單頁面。

form.php這是我在Codeigniter中的視圖文件。 我是Codeigniter的新手,無法解決此問題。

<!DOCTYPE html>
<html>
<head>

<link href="<?php echo base_url().'assets/bootstrap.min.css'; ?>" rel="stylesheet" type='text/css'>
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<!-- Latest compiled JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 <style>
 .error{color:red}
 </style>
</head>
<body>
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-3">


<h1>Student Registraion</h1>

<?php
echo form_open();
  echo form_label('First Name');
  $data=[
  'name'=>'fname',
  'class'=>'form-control',
  'id'=>'fname',
  'value'=>isset($dta['fname'])?$dta['fname']:''
  ];
  echo form_input($data);
  echo form_error('fname');

  echo form_label('Last Name');
  $data=[
  'name'=>'lname',
  'class'=>'form-control',
  'id'=>'lname',
  'value'=>isset($dta['lname'])?$dta['lname']:''
  ];
  echo form_input($data);
  echo form_error('email');
   echo form_label('Email');

  $data=[
  'name'=>'email',
  'class'=>'form-control',
  'id'=>'email',
  'value'=>isset($dta['email'])?$dta['email']:''
  ];


  echo form_input($data);

  echo form_label('Mobile Number');

  $data=[
  'name'=>'mobile',
  'class'=>'form-control',
  'id'=>'mobile',
  'value'=>isset($dta['mobile'])?$dta['mobile']:''
  ];


  echo form_input($data);
   echo form_error('mobile');
    echo form_label('Message');
  $data=[
  'name'=>'message',
  'class'=>'form-control',
  'id'=>'message',
  'value'=>isset($dta['message'])?$dta['message']:''
  ];
  echo form_textarea($data).br(1);
  echo form_submit('submit','submit','class="btn btn-success"');
  echo form_close(); 
?>
</div>
<div class="col-md-3"></div>
</div>

</body>
</html>

home.php

當我嘗試打開時,無法在Codeigniter中查看表單頁面。

在此處輸入圖片說明 當我打開插入表單時,它給我錯誤,我附上了錯誤屏幕截圖。

<?php

class home extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->model('select');
        $this->load->helper(array('form','url','html'));
        $this->load->library(array('form_validation','session'));


    }


   function records()
   {

       $ytl['dta']=$this->users->datas();
       //$this->load->view('info',$ytl);
       //echo "<pre>controller";print_r($ytl);
   }
   function primezone()
   {

           $ytl['dta']=$this->users->datas();
            echo "<pre>controller";print_r($ytl);
   }



    function form($id)
    {
    //die($id);
    if($this->input->post())
    {
        //echo"<pre>";print_r( $this->input->post());die;
        $this->form_validation->set_rules('fname','First Name','required|min_length[5]');
        $this->form_validation->set_rules('lname','Last Name','required|callback_check_picture[lname]');

        //$this->form_validation->set_rules('email','Email','required');

        $this->form_validation->set_rules('mobile','Mobile Number','required|callback_valid_phone_number[mobile]');

        $this->form_validation->set_error_delimiters('<h1 class="error">', '</h1>'); 
        if($this->form_validation->run()==True)
        {
        $data=[
        'fname'=>$this->input->post('fname'),
        'lname'=>$this->input->post('lname'),
        'email'=>$this->input->post('email'),
        'mobile'=>$this->input->post('mobile'),
        'message'=>$this->input->post('message'),
        ];
        $ytl=$this->select->insertdata($data);
        if($ytl)
        {
            $this->session->set_flashdata('message', 'Successfully Added.');
     redirect('home');
        }
        }else
        {
                //$this->load->view('form');
        }

    }
    $ytl['dta']=$this->select->getDataById($id);
    //echo "<pre>";print_r($ytl);die;
    $this->load->view('form',$ytl);


}
public function check_picture($a){
   if(!is_numeric($a))
   {
       return true;
   }else{
     $this->form_validation->set_message('check_picture', 'Please enter only char value');
     return False;
}
}




function valid_phone_number($value)
{
    $value = strlen($value);
    //echo $value;die;
    if ($value == 10) {
        return true;
    }
    else
    {
        $this->form_validation->set_message('valid_phone_number', 'Mobile number not in range'); //{10} 
    return false;
    }
}



    public function index()  

     {  
         //load the database  
         //$this->load->database();  
         //load the model  
         $this->load->model('select');  
         //load the method of model  
         $data['h']=$this->select->select();  
         //return the data in view  
         $this->load->view('fetch_view', $data);  
      }   

}


?>

select.php

這是我的模型。 當我嘗試打開時,無法在Codeigniter中查看表單頁面。

<?php

class select extends CI_model

{
    function __construct()  
      {  
         // Call the Model constructor  
         parent::__construct(); 
     $this->load->database();        
      }


    function datas()
    {
        $pzi=[
        'name'=>'pratibha',
        'email'=>'pratibha@gmail.com',
        'mobile'=>998858585,
        'address'=>'noida'

        ];

        return $pzi;
    }

    function insertdata($data)
    {
        $this->load->database();
        return $this->db->insert('student', 
        $data);
    }



    public function select()  
      {  
         //data is retrive from this query 

         $query = $this->db->get('student');  
         return $query;  
      } 

    function getDataById($id)
    {
        $this->db->select('*');
        $this->db->where('id',$id);
        $this->db->from('student');
      $query = $this->db->get()->row_array();  
         return $query; 
    }
}

?>

如果要更新記錄,則需要傳遞該記錄的ID。 然后,如果該ID存在,則可以更新,否則,請按當前操作插入。

因此,在您的控制器中:

$data=[
        'id'=>!empty($this->input->post('id')) ? $this->input->post('id') : null,
        'fname'=>$this->input->post('fname'),
        'lname'=>$this->input->post('lname'),
        'email'=>$this->input->post('email'),
        'mobile'=>$this->input->post('mobile'),
        'message'=>$this->input->post('message'),
        ];

在您的模型中:

function insertdata($data)
    {
        if(!empty($data['id'])) {
            $this->db->where('id', $data['id'])->update('student',$data);
            return $data['id'];
        } else {
            unset($data['id']);
            $this->db->insert('student',  $data);
            return $this->db->insert_id();
        }
    }

假設您的表有一個名為id的列。 根據需要更改...

您正在從控制器中調用插入函數。 調用更新功能

$this->db->where('id',$id);
$this->db->set($data);
$this->db->update('student');

暫無
暫無

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

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