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));
}
Your
dataTypeshould be:Your
datashould be:Inside of a event callback,
thisis a DOM element, so it needs to wrapped in$().or:
Which is the same as above, just less characters.
Also, in your PHP, you need to output JSON.
Then in your
successfunction, you can do: