1

I am using ajax to pass my variable into a controller. I can see the result in the success function data but when I want to echo the posted variable, I don't see any value.

This is my ajax :

id = $(this).attr("id");        

$.ajax({
    type:'POST',
    url:'mobishopp/page', 
    data: {
      'id': id 
    },
    success:function(data) {
       alert(data);
    }
});

This is my controller:

public function page()
{
    $id= $this->input->post('id');
    echo $id;
}
2
  • Improved grammar. Properly formatted code. Commented Jul 7, 2016 at 20:26
  • 1
    people are not responding and didn't feel there responsibility Commented Oct 23, 2016 at 4:14

2 Answers 2

1

In the controller when you echo $id you are not sending output to the browser window. Instead you are sending output to the success function of the ajax call. If you want it to display in the browser window you must do so in the success function.

Sign up to request clarification or add additional context in comments.

4 Comments

how can i display the result in browser?
Typically that involves using JQuery to append, insert or replace part of the DOM. If that has no meaning for you start here
You would need to share the view file (html) and explain where you want this info to show up for anybody to provide help.
@Farzaneh its your responsibility to check the answer and mark and up-vote the answer which is correct and more descriptive for you. Please do it. It will help others. Stack work in this way only.
1

To see the result of ajax request and what it sent and what is received go to developer tools in google chrome in network tab select XHR then trigger the event that generate Ajax Request the request details will be shown in result such as data been send and status of the request and the response and error if there is an error

note in php to prevent execution after echo do the following

echo $id;die();

I hope my answer would be useful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.