user1621727
user1621727

Reputation:

Ajax Post Returns Null

I'm trying to get the last inserted rows' id element after the post via ajax. But i'm not able to get the id of that data. However when i var_dump the result, the function returns the var_dumped html. What i'm missing here?

Note: Save method returns id of the last inserted.

$.ajax({
     url: '<?=site_url("admin/calendar/add");?>',
     data: 'title='+ copiedEventObject.title,
            type: "POST",
            success: function(newID){
                eventID = newID;
                alert("new id : "+eventID+" ");
            }
 });

public function add(){
      $data = (array(
        'title' => $this->input->post('title')
      ));
      return $this->calendar_m->save($data); // this is returning null
    //return var_dump($this->calendar_m->save($data));  returns var dumped id with html codes init
}

Upvotes: 0

Views: 91

Answers (2)

Nandakumar
Nandakumar

Reputation: 1101

***Returning to AJAX***

In PHP, you can't simply return your value and have it show up in the ajax response. you need to print or echo your final values. (there are other ways too, but that's getting off topic).

 echo $this->calendar_m->save($data); die();  

Upvotes: 0

SHIN
SHIN

Reputation: 386

try this

echo $this->calendar_m->save($data); 

Upvotes: 1

Related Questions