CodeIgniter Forums
how to access the data i posted through ajax on my controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: how to access the data i posted through ajax on my controller (/showthread.php?tid=68712)



how to access the data i posted through ajax on my controller - Lestah - 08-16-2017


Code:
$.ajax({
                    type: "POST",
                    url: '<?php echo site_url('login/identifying_usertype'); ?>',
                    data: { 'email' : email, 'password' : password },
                    success: function(response){
                        
                        //console.log(response);
                        //alert(email);
                        console.log(response);
                        $('#myModal').modal('hide');
                        },
                        error: function (XHR, status, error){
                            console.log('error', error);
                        }
                    });

and this is my controller
Code:
public function identifying_usertype()
{
    $email = $_POST['email'];

     echo "$email";
}
I can't get the posted value on my ajax Is there something I'm missing? what could be wrong?


RE: how to access the data i posted through ajax on my controller - InsiteFX - 08-16-2017

Maybe $this->input->post('name');


RE: how to access the data i posted through ajax on my controller - skunkbad - 08-16-2017

(08-16-2017, 09:15 AM)InsiteFX Wrote: Maybe $this->input->post('name');

A little background on this question ... it comes from a stack overflow question that I was trying to help Lestah with. If he posts to the Welcome controller, then $this->input->post('name') works, but if he posts to any other controller then $this->input->post('name') returns NULL. It's very bizarre. Have you heard of such a thing?


RE: how to access the data i posted through ajax on my controller - InsiteFX - 08-16-2017

Nope, the only thing I can think of is maybe he needs to clean his web browsers cache out.

If he specify's the correct controller then it should post to it.

Ahhh, look at his ajax url, he is only using single qoutes

Code:
// should be like this.
url: "<?php echo site_url('login/identifying_usertype'); ?>",

Not sure if that's the problem but it should be like I showed.


RE: how to access the data i posted through ajax on my controller - skunkbad - 08-16-2017

(08-16-2017, 11:31 AM)InsiteFX Wrote: Nope, the only thing I can think of is maybe he needs to clean his web browsers cache out.

If he specify's the correct controller then it should post to it.

Ahhh, look at his ajax url, he is only using single qoutes

Code:
// should be like this.
url: "<?php echo site_url('login/identifying_usertype'); ?>",

Not sure if that's the problem but it should be like I showed.

We spent a lot of time trying to figure this out yesterday. Didn't suggest cleaning the browser cache, but jumped through a lot of hoops:

https://stackoverflow.com/questions/45704888/how-can-i-check-if-i-got-the-value-from-my-ajax-to-my-controller/45705168?noredirect=1#comment78372679_45705168


RE: how to access the data i posted through ajax on my controller - ciadvantage - 08-16-2017

Would you check your form if the email input with 'attribute' name='email' and  password input name='password'?

Code:
<form>
  <p>Email:<input type='text' name='email' />
</p>
<p><input type='password' name='password' /></p>
<input type='submit' value='Submit' />
</form>
<script>
   var data=$('form').serialize();
   var url = 'your_url'; // ie. login/identifying_usertype
    $('form').on('submit',function(e){
              e.preventDefault();
  
   $.post(url,data,function(response){
     
     },'json').done(function(response){
         console.log(JSON.stringify(response));// should have response data here
     }).fail(function(){
         //some action as you please
     });//end post
     });//end submit
</script>
 
  then in the controller:
  
Code:
public function identifying_usertype()

 $data=$this->input->post(NULL,TRUE);

 echo json_encode($data); //should response as json string at view
}

 Try that code and see if  you see the response.  I m pretty sure you should!
 Good luck