2017-03-24 5 views
0

so hier ist mein Problem, ich erstelle meine eigene Upvote und Downvote-Funktion in meinem Schulprojekt. Alles was ich möchte ist, wenn ich auf ein Pfeil-hoch-Bild klicke, wird ein Datensatz in die Datenbank eingefügt. Es wird einen Datensatz in die Tabelle upvote in meiner Datenbank hinzufügen.Wie kann ich Datenbank-Datensatz einfügen, wenn eine Schaltfläche geklickt wird - Codeigniter, Jquery, Ajax

<script type="text/javascript"> 

$(document).ready(function() { 



    $("#try").click(function(){ 
     var username = $("#username").val(); 
     var thread_id = $("#thread_id").val(); 
     var vote = $("#vote").val(); 

     $.ajax(
      { 
       type:"post", 
       url: "<?php echo site_url();?>/Users/add_vote/", 
       data:{ username:username, thread_id:thread_id, vote:vote}, 
       success:function(data) 
       { 
        alert("POST SUBMITTED"); 

       } 


      }); 



}); 
    }); 

und hier ist meine Form

<div id = "active_upvote_div"> 
       <form> 
        <input type="hidden" name = "username" id="username" value= "try"> 
        <input type="hidden" name="thread_id" id="thread_id" value= "1"> 
        <input type="hidden" name="vote" id="vote" value= "1"> 
       <button type="submit" id="try"><img src="<?php echo base_url();?>/public/images/upvote.png" height:"25px" width="25px"/> </button> 
       </form> 
      </div> 

mein Controller ruft nur das Modell

Das ist mein Modell

public function ajax() 
     { 
     $add_user = array(
     'username'  => $this->input->post('username'), 
     'thread_id' => $this->input->post('thread_id'), 
     'vote' => $this->input->post('vote'), 

      ); 

     $this->db->insert('thread_upvote', $add_user); 
     } 

für eine sofortige Reaktion der Hoffnung: (

Antwort

0

In Codezeichen $this->input->post() ist äquivalent zu $_POST, die Array von geposteten Namen als key/value Paar macht.

In Ihrer Funktion controller's add_vote() dies tun ...

public function add_vote() 
{ 
$data = $this->input->post(); 
$this->load->model('model_namel');//loads model 
$this->model_name->ajax($data);//call model's function with data 
} 

Dann in model's ajax() Funktion

public function ajax($data=array()) 
{ 
    $this->db->insert('thread_upvote', $data);// inserts into database 
} 
0

In Ihrem Ajax:

$(document).ready(function() { 

    $("#active_upvote_div").click(function(){ 
     var username = $("#username").val(); 
     var thread_id = $("#thread_id").val(); 
     var vote = $("#vote").val(); 

     $.ajax({ 
      type:"POST", 
      dataType: 'json', //add this to call json 
      url: "<?php echo site_url();?>/Users/add_vote/", 
      data:{ username:username, thread_id:thread_id, vote:vote}, 
      success:function(data) 
      { 
       if(data.success == true){ 
        alert("POST SUBMITTED"); 
       } else{ 
        alert("Failed to Post"); 
       } 
      } 
     }); 
    }); 
}); 

In Ihrem Controller:

Class Users extends CI_Controller{ 
    public function add_vote() 
    { 
     $result = $this->name_of_model->ajax(); //call the ajax function in you model 
     echo json_encode($result); //check the return if true or false and pass to json 
    } 
} 

In Ihrem Modell:

public function ajax() 
{ 
    $add_user = array(
     'username'  => $this->input->post('username'), 
     'thread_id' => $this->input->post('thread_id'), 
     'vote' => $this->input->post('vote'), 
    ); 

    if($this->db->insert('thread_upvote', $add_user)) 
    { 
     return true; 
    } else{ 
     return false; 
    } 
} 
Verwandte Themen