Welcome Guest, Not a member yet? Register   Sign In
Get Mysql insert_id in java
#1

Hi all im new to codeignighter and seem to be having ap roblem with retriving the last insert_id.

what im doing

first of all im not using a form

i catch the button id from java and call my controller function like so

Code:
$('#statusupdate').click(function(){  // when user click no move to next profile and save choice to db
var val_post = $("#msgstatus").val();
$.get("http://mydomain/xxxxxx/mycontroller/set_post/"+ val_post,
function(data){
            
      alert('success');
            });
      });

my controller then calls a function in my model like so

Code:
function set_post($message){
   $user_id = $this->flexi_auth->get_user_id();
   $this->load->model('members_model');
   $data['postid'] = $this->members_model->add_status_post($user_id,$message,'0','0');
   return $data;    
}

my model looks like so

Code:
public function add_status_post ($guid,$message,$type=0,$data=0) {
 
  $dt = date('Y-m-d H:i:s');
  $data = array(
     'tmln_user_id' => $guid ,
     'tmln_message' => urldecode($message) ,
     'tmln_type' => $type,
     'tmln_data' => $data,
     'tmln_date_added' => $dt
  );

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

return  $this->db->insert_id();  
   }

how can i get this user id in my java script ?
by the way the row does get inserted Smile
thanks
Reply
#2

ok all just to let you know i found a solution

i added these two lines to my controller function

Code:
$this->output->set_content_type('application/json');
    $this->output->set_output(json_encode($data))

and changed my java like so

Code:
$('#statusupdate').click(function(){  // when user click no move to next profile and save choice to db
var val_post = $("#msgstatus").val();
              
  
                 $.get("http://www.leap-it.be/tangoo2/auth_public/set_post/"+ val_post,
     function(data){
                
       alert(data.postid);
            });


if my coding looks bad let me know

thx
Reply
#3

If you are sending something back in an ajax request, you don't "return" it. That returns something back to your PHP script and NOT your browser. You output/echo something back to the browser.

Instead of
Code:
return $data;

Try
Code:
echo json_encode($data);

Then in your javascript success event:
Code:
function(data){
     //alert('success');  
     console.log(data);
});

Then check the js console of you browser. You should see a json object which contains 'post_id' and its value.
Reply
#4

FYI, Java is a different thing from JavaScript ... if you continue to call it that way, fellow programmers will make fun of you. Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB