Welcome Guest, Not a member yet? Register   Sign In
Ajax post function problem
#1

[eluser]jacobson[/eluser]
Hello, I have a problem ... I have a code of function in my view file :
Code:
function enroll(id){
        $.post(
            'http://localhost/index.php/main/ajax_function/'+id,
            function(response) {  
            if (response == 1) {
               alert('Enrolled');
            } else {
               alert('Not Enrolled');
            }
            }
        );
    }

and my controller function

Code:
function ajax_function($id){
        $data = array(
            'userid' => $this->session->userdata('username'),
            'id' => $id
        );
        $this->db->insert(enroll, $data);
    }

when I start the js function it doesnt work. Can you see mabe any mistake in my code, why it is not working ?
thx for responding Smile
#2

[eluser]JHackamack[/eluser]
One thing i see is that you don't have your table name wrapped in quotes:
Code:
$this->db->insert('enroll', $data);
#3

[eluser]jacobson[/eluser]
misclick i have the " ' " there but it still doesnt work... :/
#4

[eluser]JHackamack[/eluser]
Have you used Firebug on Firefox or Web Inspector on Safari to view the ajax request? Does the server return anything about an error that might have occurred?
#5

[eluser]CroNiX[/eluser]
I think you need to grab the ID from the URI segment, not passed to the function.
Code:
function ajax_function(){
  $data = array(
    'userid' => $this->session->userdata('username'),
    'id' => $this->uri->segment(3)
  );
  $this->db->insert(enroll, $data);
}
#6

[eluser]CroNiX[/eluser]
Also, you are testing for (response == 1) but you don't return anything from ajax_function().
#7

[eluser]jacobson[/eluser]
ok but response is responsible only for alerting that it worked but I can see in phpmyadmin that there are no records.

I've changed my code:
view:
Code:
function enroll(id){
        $.post(
            'http://localhost/index.php/main/ajax_function/'+id
        );
    }

controller:
Code:
function ajax_function(){
        $this->myselection_model->abba();
    }

and model:
Code:
function abba(){
        $data = array(
            'id' => $this->uri->segment(3),
            'userid' => $this->session->userdata('username')
        );
        $this->db->insert('enroll', $data);
    }

and still nothing... Tongue i've also tried passing variable to the model (id) from controller but also nothing

__________________________________
I've used firebug nad I found a problem... it was my bad because I've tried to post varchar(10) and in mysql i had only varchar(5). Thank you all for your help.
#8

[eluser]dUspan[/eluser]
ahm

why dont you try this

Code:
function enroll(id){
        $.post(
            'http://localhost/index.php/main/ajax_function/',{id: id},function(response){
         //your response here
          });
        
    }

i hope it helps Big Grin
#9

[eluser]masrodjie[/eluser]
Use relative URL:
Quote:function enroll(id){
$.post(
'/index.php/main/ajax_function/'+id
);
}




Theme © iAndrew 2016 - Forum software by © MyBB