ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Ajax Form Duplicate Insert Into

July 26, 2011 10:16pm

Subscribe [3]
  • #1 / Jul 26, 2011 10:16pm

    FREEZA's avatar

    FREEZA

    15 posts

    Hello Community,
    i have a big problem with my Form. When i press the submit button, the insert into MYSQL function writes two entrys… not one Entry! So this Entry is duplicate in my database…

    Please Help!!!!

    I dont know why….


    VIEW:

    <?php 
                   // Form erstellen
    
                    echo form_open('', 'id="entryForm"');
                    echo form_input('betreff', 'Betreff', 'id="betreff"');
                    echo form_textarea('content', 'Content', 'id="content"');;
                    echo form_submit('submit', 'Absenden', 'id="submit"');
                    echo form_close();
    
      ?> 
       
     
           // JAVASCRIPT
          
            $('#submit').click(function() {
      
             var form_data = {
              betreff : $('#betreff').val(),
              content : $('#content').val(),
              ajax : '1'
              };
      
         
      $.ajax({
         url: "<?php echo site_url('entry/doEntry'); ?>",
         type: 'POST',
         data: form_data,
         success: function(msg) {
           $('#write-entry').html(msg);
         }
       });
        
      return false;
     });
      
    // END JAVASCRIPT


    CONTROLLER:

    <?php
    
    class Entry extends CI_Controller{
    
        
         public function doEntry(){
             
             // Model laden
             $this->load->model('entry_model');
             
             // Wurde das Ajax Formular benutzt?
             if ($this->input->post('ajax')) {
                 
                 // Wenn Insert Erfolgreich
                 if($this->entry_model->insert()){
                     
                     echo "SUCCESS!";
                     
                 };
             }
    
         }
    
    ?>


    MODEL:

    <?php
    
    class Entry_model extends CI_Model{
         
         function insert(){
             
             $data = array(
                 
                 'headline' => $this->input->post('betreff'),
                 'content' => $this->input->post('content')
                 
             );
             
             $insert = $this->db->insert('entry', $data);
             
             return $insert;
             
             
         }
         
    }
    ?>
  • #2 / Jul 26, 2011 11:51pm

    memVN

    30 posts

    I think it’s error in your model, you can edit again

    <?php
    
    class Entry_model extends CI_Model{
         
         function insert(){
             
             $data = array(
                 
                 'headline' => $this->input->post('betreff'),
                 'content' => $this->input->post('content')
                 
             );
             
             $insert = $this->db->insert('entry', $data);
             
             //return $insert;
             
             
         }
         
    }
    ?>
  • #3 / Jul 27, 2011 12:03am

    appleboy's avatar

    appleboy

    38 posts

    Please modified your model code as the following sample

    $this->db->insert('entry', $data);
    return TRUE;

    You can find _insert function from system/database/drivers/mysqli/mysqli_driver.php

    /**
         * Insert statement
         *
         * Generates a platform-specific insert string from the supplied data
         *
         * @access  public
         * @param   string  the table name
         * @param   array   the insert keys
         * @param   array   the insert values
         * @return  string
         */
        function _insert($table, $keys, $values)
        {
            return "INSERT INTO ".$table." (".implode(', ', $keys).") VALUES (".implode(', ', $values).")";
        }
  • #4 / Jul 28, 2011 9:55am

    FREEZA's avatar

    FREEZA

    15 posts

    Hi,

    the problem is that FIREBUG ( Mozilla ) shows a duplicate request with POST Data. When i click on Submit, he sends the Post Data two times

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases