CodeIgniter Forums

Full Version: how to access the data i posted through ajax on my controller
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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?
Maybe $this->input->post('name');
(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?
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.
(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/4570...9_45705168
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