简体   繁体   中英

inserting data into database using jquery ajax submit button codeigniter

i am trying to insert data into the database using the codeigniter framework, i tried many solutions i couldn't find a problem ., can somebody suggest me the proper answer after adding $this->model->('contact_model'); my page is blank

my controller pages.php

  <?php

  class pages extends CI_Controller{
  function index{
  $this->load->model('contact_model');      
  $this->load->view('pages/index');
          }
}
 ?>

enter code here

my view index.php

<form id="submit" action="<?php echo site_url();?>/models/contact_model">

  <input type="text" id="name" >    
  <input type="email" id="email" >
  <textarea placeholder="Message"  id="message"></textarea>
  <button type="button" id="submit1">Send</button>
  <a id="ack"></a>          
</form>

my model contact_model.php

<?php
 class contact_model extends CI_Model{    

    function insert_entry()

    {
        $data = array(
        'name' => 'name' ,
        'email' => 'email' ,
        'message' => 'message');

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

?>

custom.js file

$("#submit1").click(function){
$.post($("#submit").attr("action"),
       $("#submit :input").serializeArray(),


       function(data)
       {

       $("div#ack").html(data); 
       }); 

$("#submit1").submit(function(){
return false;
   // window.location.href="/application/models/";
 });


 });

try this :3 this is ajax to post data in page to models ...

$.ajax({
    url : "<?php echo base_url();?>/pages/",
    type : 'POST',
    data :{
           "name":$("#nametags").val(),
           //more in here
    },
    success : function(data) {
});

in controler

 class pages extent CI_Controller{
function index() {}
function insert_data(){
       //call function insert data
 $data = array(
    'name' => 'name' ,
    'email' => 'email' ,
    'message' => 'message');

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

your url is: /insert_data

in action url first parameter won't be model it would be controller name

like follwoing <?php echo site_url();?>/controller_name/method_name

in your case use <?php echo site_url();?>/pages/

$.ajax({
url : "<?php echo site_url();?>/pages/insert_data",
type : 'POST',
data :{"name":$("#name").val(),"email":$("#email").val(),"message":$("#message").val()},
success : function(data) {

$("div#ack").html(data);

});

and create a function in controller

function insert_data()
{
  $this->load->model('contact_model');
  $this->contact_model->insert_entry();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM