CodeIgniter and jquery



>** 好吧,这是我的prooblem,我显示了我的数据库中的用户表 我,但是当我单击添加按钮时,我卡在添加部分,我填写了信息,但我无法将它们发送到我的数据库我不知道为什么,但我认为它与post方法有关, 你能帮帮我吗**

professeuredit_view
<html>  
 <head>  
   <title><?php echo $title; ?></title>  
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />  
      <script src="https://cdn.datatables.net/1.10.12/js/jquery.dataTables.min.js"></script>  
      <script src="https://cdn.datatables.net/1.10.12/js/dataTables.bootstrap.min.js"></script>            
      <link rel="stylesheet" href="https://cdn.datatables.net/1.10.12/css/dataTables.bootstrap.min.css" />  
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>  
   <style>
           body  
           {  
                margin:0;  
                padding:0;  
                background-color:#f1f1f1;  
           }  
           .box  
           {  
                width:900px;  
                padding:20px;  
                background-color:#fff;  
                border:1px solid #ccc;  
                border-radius:5px;  
                margin-top:10px;  
           }  
      </style>  
 </head>  
 <body> 
  <div class="container box">  
           <h3 align="center"><?php echo $title; ?></h3><br />  
            <div class="table-responsive">
            <br />  
                <button type="button" data-toggle="modal" data-target="#userModal" class="btn btn-info btn-lg">Add</button>  
                <br /><br /> 
            <table id="prof_data" class="table table-bordered table-striped">
                       <thead>
                          <tr>  
                               <th width="10%">Nom</th>  
                               <th width="35%">Prenom</th>  
                               <th width="35%">Telephone</th> 
                               <th width="10%">Email</th>  
                               <th width="35%">Departement</th>  
                               <th width="20%">Fonctionnalite</th> 
                               <th width="30%">Grade</th>
                               <th width="5%">Edit</th>  
                               <th width="5%">Delete</th> 
                               </thead>
                          </tr> 
                        </thead>
                    </table>
          </div>
   </div>
</body>
</html>
<div id="userModal" class="modal fade">  
      <div class="modal-dialog">  
           <form method="post" id="user_form">  
                <div class="modal-content">  
                     <div class="modal-header">  
                          <button type="button" class="close" data-dismiss="modal">&times;</button>  
                          <h4 class="modal-title">Add User</h4>  
                     </div>
                     <div class="modal-body">  
                          <label>Enter le nom</label>  
                          <input type="text" name="nom" id="nom" class="form-control" />  
                          <br />  
                          <label>Enter le prenom</label>  
                          <input type="text" name="prenom" id="prenom" class="form-control" />  
                          <br />  
                          <label>Enter le telephone</label>  
                          <input type="text" name="telephone" id="telephone" class="form-control" />  
                          <br />  
                          <label>Enter l'email</label>  
                          <input type="email" name="email" id="email" class="form-control" />  
                          <br /> 
                          <label>Enter le departement</label>  
                          <select name="departement" class="form-contol">
                          <option selected>Informatique
                          <option>Mathématiques
                          <option>Chimie
                          <option>Physique
                          <option>Biologie
                          <option>Géologie
                          </select>  
                          <br />  
                          <label>Enter la fonctionnalite</label>  
                          <select name="fonctionnalite" class="form-contol">
                          <option selected>Adiministrateur 
                          <option>Chef de Départ
                          <option>Coordonnateur de filière 
                          <option>Professeur 
                          </select>  
                          <br />
                          <label>Enter le grade</label>
                          <select name="grade">
                          <option selected>PA 
                          <option>PH
                          <option>PES 
                          <option>V 
                          <option>C
                          </select>   
                          <br />  
                     </div>  
                      <div class="modal-footer"> 
                         <input type="hidden" name="prof_id" id="prof_id"  />
                         <input type="submit" name="action" class="btn btn-success" value="Add" />  
                          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>  
                     </div>  
                </div>  
           </form>  
      </div>  
 </div>  
 <script type="text/javascript" language="javascript" >  
 $(document).ready(function(){ 
  var dataTable = $ ('#prof_data').DataTable({
    "processing":true,
    "serverSide":true,
    "order":[],
    "ajax":{
            url:"<?php echo base_url() . 'professeuredit/fetch_user'; ?>",  
                type:"POST",
                dataType    : 'json'
           }, 
            
           "columnDefs":[  
                {  
                     "targets":[0],  
                     "orderable":false,  
                },  
           ],
                                           });
$(document).on('submit', '#user_form', function(event){  
           event.preventDefault();  
           var firstname  = $('#nom').val();  
           var lastName   = $('#prenom').val(); 
           var tel        = $('#telephone').val();  
           var ema        = $('#email').val(); 
           var depar      = $('#departement').val();  
           var fonct      = $('#fonctionnalite').val();  
           var gra        = $('#grade').val();  
           if(firstName != '' && lastName != '' && tel != '' && ema != '')  
           {  
                $.ajax({  
                     url:"<?php echo base_url() . 'professeuredit/user_action'?>",  
                     method:'POST',  
                     data:new FormData(this),  //data we want to send to server 
                     contentType:false,  
                     processData:false,  
                     success:function(data)  
                     {  
                          alert(data);  
                          $('#user_form')[0].reset();  
                          $('#userModal').modal('hide');  
                          dataTable.ajax.reload();  
                     }  
                });  
           }  
           else  
           {  
                alert("Fields are Required");  
           }  
      }); 
    
 

});
</script>
professseuredit_model
<?php  
 class Professeuredit_model extends CI_Model  
 {
    var $table = "professors";  
      var $select_column = array("id", "nom", "prenom", "telephone", "email", "departement", "fonctionnalite","grade");  
      var $order_column = array(null,"nom", "prenom", "telephone", "email", "departement", "fonctionnalite","grade");  
      
      public function make_query()  
      {     
           $this->db->select($this->select_column);  
           $this->db->from($this->table);  
           if(isset($_POST["search"]["value"]))  
           {  
                $this->db->like("nom", $_POST["search"]["value"]);  
                $this->db->or_like("prenom", $_POST["search"]["value"]);  
           }  
           if(isset($_POST["order"]))  
           {  
                $this->db->order_by($this->order_column[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);  
           }  
           else  
           {  
                $this->db->order_by('id', 'DESC');  
           }  
      } 
      public function make_datatables()
      {  
           $this->make_query();  
           if($_POST["length"] != -1)  
           {  
                $this->db->limit($_POST['length'], $_POST['start']);  
           }  
           $query = $this->db->get();  
           return $query->result();  
      }
       
       public  function get_filtered_data()
       {  
           $this->make_query();  
           $query = $this->db->get();  
           return $query->num_rows();  
      } 
      public function get_all_data()  
      {  
           $this->db->select("*");  
           $this->db->from($this->table);  
           return $this->db->count_all_results();  
      }
      
      
      public  function insert_crud($data)  
      {  
           $this->db->insert('professors', $data);  
      }
       
     
}
?>
      
professeuredit 
<?php  
 defined('BASEPATH') OR exit('No direct script access allowed');  
 class Professeuredit extends CI_Controller { 
 	  public function index(){
                               $data["title"]="TABLE DES PROFESSEURS ";
                               $this->load->view('professeuredit_view',$data);
 	                               }
 	  public function fetch_user(){  
           $this->load->model("professeuredit_model");  
           $fetch_data = $this->professeuredit_model->make_datatables();  
           $data = array();  
           foreach($fetch_data as $row){ 
           	    $sub_array = array();  
                $sub_array[] = $row->nom;  
                $sub_array[] = $row->prenom; 
                $sub_array[] = $row->telephone;  
                $sub_array[] = $row->email; 
                $sub_array[] = $row->departement;  
                $sub_array[] = $row->fonctionnalite;  
                $sub_array[] = $row->grade;  
                $sub_array[] = '<button type="button" name="update" id="'.$row->id.'" class="btn btn-warning btn-xs update">Update</button>';  
                $sub_array[] = '<button type="button" name="delete" id="'.$row->id.'" class="btn btn-danger btn-xs delete">Delete</button>';  
                $data[] = $sub_array;  
           }  
           $output = array(  
                "draw"                  =>     intval($_POST["draw"]),  
                "recordsTotal"          =>     $this->professeuredit_model->get_all_data(),  
                "recordsFiltered"       =>     $this->professeuredit_model->get_filtered_data(),  
                "data"                  =>     $data  
           );  
           echo json_encode($output);  
           }
    public function user_action()
    {
                if($_POST["action"] == "Add")
                {
                  $insert_data=array(
                         'nom'            => $this->input->post('nom'),
                         'prenom'         => $this->input->post('prenom'),
                         'telephone'      => $this->input->post('telephone'),
                         'email'          => $this->input->post('email'),
                         'departement'    => $this->input->post('departement'),
                         'fonctionnalite' => $this->input->post('fonctionnalite'),
                         'grade'          => $this->input->post('grade')
                        );
                 
                $this->load->model('professeuredit_model');  
                $this->professeuredit_model->insert_crud($insert_data);  
                echo 'Data Inserted';
                 }
      }
                 
 }
 ?>

 

更新代码中的以下代码

$(document).on('submit', '#user_form', function(event){
    event.preventDefault();
    if($.trim($('#nom').val()).length > 0 && $.trim($('#prenom').val()).length > 0 && $.trim($('#telephone').val()).length > 0 && $.trim($('#email').val()).length > 0){            
        $.ajax({
            url:"<?= base_url('professeuredit/user_action'); ?>",  
            method:'POST',  
            data: $(this).serialize(),
            dataType : 'html',
            success:function(data){  
                alert(data);
                $('#user_form')[0].reset();  
                $('#userModal').modal('hide');  
                dataTable.ajax.reload();  
            },
            error: function(error){
                console.log(error);
            }
        });
    }else{
        alert("Fields are Required");  
    }
});
public function user_action(){
    if($this->input->post('action')){
        $insert_data = $this->input->post();
        unset($insert_data['prof_id'],$insert_data['action']);
        $this->load->model('Professeuredit_model','professeur');
        $this->professeur->insert_crud($insert_data);  
        echo 'Data Inserted';
    }
}

最新更新