Passing form data to controller using AJAX and jquery with Codeigniter

22,579

Solution 1

As @David Knipe already said, you should wait for the DOM to be ready before trying to access one of its elements. Moreover, you likely have an e is undefined error, since you're not passing the event reference to the click handler:

<script>   //no need to specify the language
 $(function(){
  $("#submitbutton").click(function(e){  // passing down the event 

    $.ajax({
       url:'http://localhost:8888/index.php/trial/insert_into_db',
       type: 'POST',
       data: $("#myForm1").serialize(),
       success: function(){
           alert("success");
           $('#email').val('');
           $('#qText').val('');
       },
       error: function(){
           alert("Fail")
       }
   });
   e.preventDefault(); // could also use: return false;
 });
});
</script>

To be more complete, your query is vulnerable, and you might be getting an error there. Make use of the advantage of having a framework beneath you:

function insertQ(){
    $email = $this->input->post('email');
    $qText = $this->input->post('qText');
    $this->db->insert('questions', array('email' =>$email, 'text' => $text));
}

^_ I'm guessing the column names since you didn't specify them

Solution 2

Looks like you've forgotten to use $.ready. As it is, the javascript runs before the page has loaded, which means it can't find the page element from $("#submitbutton"). Try this:

$(document).ready(function() {
    $("#submitbutton").click(function(){
    ....
    ....
});
Share:
22,579
user2796352
Author by

user2796352

Updated on July 09, 2022

Comments

  • user2796352
    user2796352 almost 2 years

    I am trying to post the data from this form into the database. I have tried some tutorials with no success. Here is my code. Any ideas?

    View:

    <form method="post" name="myForm1" id="myForm1" enctype="multipart/form-data" >
    Email: <input type="text" name="email" id="email">
    Question: <input type="text" name="qText" id="qText">
    <input id="submitbutton" type="submit">
    </form>
    

    AJAX (in the view, right below the form)

    <script type='text/javascript' language='javascript'>
    
    $("#submitbutton").click(function(){
    
    
    $.ajax({
           url:'http://localhost:8888/index.php/trial/insert_into_db',
           type: 'POST',
           data: $("#myForm1").serialize(),
           success: function(){
               alert("success");
           },
           error: function(){
               alert("Fail")
           }
       });
       e.preventDefault();
    });
    
    
    </script>
    

    Controller

      function insert_into_db(){
        $this->load->model('insert_db');
        $this->insert_db->insertQ();  
    }
    

    Model

     class Insert_db extends CI_Model{
    
        function insertQ(){
            $email = $_POST['email'];
            $qText = $_POST['qText'];
            $this->db->query("INSERT INTO questions VALUES('','$email','$qText','','')");
        }
     }