DCHP
DCHP

Reputation: 1131

Jquery post with codeigniter

I am moving over from wordpress to codeigniter but i am struggling to call a controller function from a jquery post here are my files.

in my home view i have this

<a class="add_playlist" href="5657584"><img src="http://icons.iconarchive.com/icons/dryicons/simplistica/32/add-icon.png" alt="playlist"/></a>

and this in the footer

    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script> 
    <script type='text/javascript'>
    $('.add_playlist').live('click', function() {

        alert('add');

        var video_url = $(this).attr('href');

            $.post("http://localhost/code/index.php/home/add_playlist", {video_url: video_url}, function(response) {
                console.log(response);

        });

    return false;

});

So what i am trying to do is send this to a function called add_playlist in my home controller. here is the code in my home controller.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    function add_playlist(){

        $this->load->model('home_model');

        // if HTTP POST is sent, add the data to database
        if($this->input->post('video_url')) {

        $video_url = $this->input->post('video_url');

        $this->home_model->add($video_url);

        } else {

        }
    }

}


    return false;

});
</script> 

And here is my home model

<?php

class home_model extends CI_Model {

    function add($data) {

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

    }

}

So all i want it to do is add the href to the database can someone please scan this and helps

Thanks

this is the error its throwing

add_playlistPOST http://localhost/code/index.php/home/add_playlist 500 (Internal Server Error)

Upvotes: 0

Views: 365

Answers (1)

stormdrain
stormdrain

Reputation: 7895

The code in your home controller has rogue javascript at the bottom:

    return false;

});
</script> 

?

Should look like this:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

    function add_playlist(){

        $this->load->model('home_model');

        // if HTTP POST is sent, add the data to database
        if($this->input->post('video_url')) {

        $video_url = $this->input->post('video_url');

        $this->home_model->add($video_url);

        } else {

        }
    }

}

Upvotes: 2

Related Questions