2

Ok, Im a real newbie when it comes to ajax and json ... Im trying to figure it out in my codeigniter project.

Ive written something simple to start, just to bring up an alertbox, but it doesnt seem to be working, if someone could let me know where im going wrong, that would be grand.

In my view i have the following code.

$('.users').change(function(){
    $.ajax({
        type: "POST",
        url: "/edituser/returndata",
        data: {id: this.find(':selected').val()},
        dataType: json,
        success: function(data){
            alert(data);
        }
    });
});

in the edituser/returndata controller, i just simply have the following

function returndata(){
    echo $_POST['id'];
}

I know this will look pretty stupid to some people, but im still trying to figure it out, if someone could help :)

Cheers

----------------- UPDATED CODE BELOW

<script type="text/javascript" charset="utf-8">
$('#users').live('change', function(){

    $.ajax({
        type: "POST",
        url: "/edituser/returndata",
        data: {id: $(':selected', this).val()},
        dataType: 'json',
        success: function(data){
            alert(data.id);
        }
    });
});                     
</script>

Controller code

function returndata()
{
    $ID = $this->input->post('id');  // Use this instead of $_POST['id']
    echo json_encode(array('id'=>$ID));
}

2 Answers 2

3

Your dataType should be:

dataType: 'json',

Your data should be:

data: {id: $(this).find(':selected').val()},

Inside of a event callback, this is a DOM element, so it needs to wrapped in $().

or:

data: {id: $(':selected', this).val()},

Which is the same as above, just less characters.

Also, in your PHP, you need to output JSON.

function returndata(){
  $ID = $this->input->post('id');  // Use this instead of $_POST['id']
  echo json_encode(array('id'=>$ID));
}

Then in your success function, you can do:

alert(data.id);
Sign up to request clarification or add additional context in comments.

14 Comments

If the dropdown element is being rendered by jquery also ... I have to use a bind or something aswell dont i?
@Rocket ... I have no idea hahahaha this is all new to me, but i did go through this before, basically my dropwdown menus are being graphically rendered by jquery as well, someone told me to bind it or something like that ... does that make any sense?
@BigJobbies: Yes, it makes sense. You need to change $('.users').change(function(){ to $('.users').live('change', function(){
Ahhh yes .. live what the one haha ... I have addded that all in, it still doesnt seem to be returning the alertbox though :S
@Rocket - Basically what ive done is just add a little if statement saying that if the current controller equals users then dont load the pretty looking dropdowns ... everything works perfect now :)
|
1

UPDATE Disregard the answer. I thought the JSON was sent as a string, but it's not, as pointed out by Rocket. It is converted to url encoded value pairs. I'm leaving the answer up just in case someone thought the same thing as me....


The incoming JSON is not a request parameter, you need to read the body of the request

$json = json_decode(trim(file_get_contents('php://input'));
$id = $json->id;

5 Comments

Hey, thanks for the response ... Im having a bit of trouble understanding how i implement thost 2 lines into my code though ... Is there any chance you could shoot me some example code? thanks
What does this code do? Why are you using json_decode? The id value is being POSTed to the PHP file, not sent as JSON.
I did show you example code. The first code chunk (2 lines) can be pasted verbatim into your action handler. $id should hold the value you want. The second code of chunk can be added to your base controller so that all your controllers can call it instead of duplicating the work
dataType: json is the format of the data received from the server, not sent to it.
I just realized that jQuery.ajax converts the json into url encoded parameters. I'm used to Ext-JS which allows you to send JSON. Therefore, my answer is completely bogus/

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.