CodeIgniter Forums
Jquery - issue with post method - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Jquery - issue with post method (/showthread.php?tid=54790)



Jquery - issue with post method - El Forum - 09-25-2012

[eluser]Piekarz[/eluser]
Hi, I try to use jQuery in my code. I got the html select/option list like this:
Code:
<select class="emaillist">
    <option value="emails"></option>
&lt;!-- list of email adresses... --&gt;
    </select>
I create jQuery function like this:
Code:
$("select.emaillist").change(function() {
$.ajax({
        type: "POST",
        url: "http://localhost/mgr/index.php/welcome/post_ajax",
        dataType: "json",
        data: "email="+$(this).text(),
        cache:false,
        success:
          function(data){
            $("div.leftecho").text(data);
          }
        
        });
        return false;
});
and in my welcome controller I put this code:
Code:
public function post_ajax(){
            if(isset($_POST['email'])){
                $this->session->set_userdata('selectedemail',$_POST['email']);
                return true;
            }
        }

Could anyone say my why this code doesn't work? When I add this code to my jQuery function:
Code:
statusCode: {
    404: function() {
      alert("page not found");
    }
  }
It always shows alert message, why ajax can't verify my url well? Is it the problem?


Jquery - issue with post method - El Forum - 09-25-2012

[eluser]ojcarga[/eluser]
Are you sure you can access this url "http://localhost/mgr/index.php/welcome/post_ajax"?

Try to access it through the browser and set an "else" for you to check:

Code:
public function post_ajax(){
    if(isset($_POST['email'])){
        $this->session->set_userdata('selectedemail',$_POST['email']);
        //return true;
        echo "COOL!"; //instead of return you must echo something
    }else{
        echo "Direct access...";
    }
}

Cheers!


Jquery - issue with post method - El Forum - 09-25-2012

[eluser]Piekarz[/eluser]
Yeah, I am sure. I changed code like you wrote and i got a message in browser:
"Direct access..."


You know, I don't really know what I changed but it starts to work o.O
Thx for reply.

The most funny thing is that I tried to resolve this issue from few days and when I asked you, it starts to work. Epic weird but thx for help :]