Reputation: 2074
I have a button in my page, when I click on this button it redirect me to this link readMessage/id
.
I want when I click on it to stay in the same page, I don't want my page to be reloaded or something else.
The link readMessage/id
will update the database, which is a function in a controller :
function readMessage($id) {
$this->message_module->readMessage($id);
$this->index();
}
This function will call the readMessage
function in the message
module :
function readMessage($id) {
$data = array('is_read' => 1);
$this->db->where('id',(int)$id)
->update($_tbl_msg, $data);
}
This is my button :
<button class="testButton" data-id="<?php echo $message->id; ?>">Test</button>
And this is the code I tried :
$('.testButton').click(function(){
// get jquery object access to the button
var $thisButton = $(this);
var form_data = {
id : $thisButton.data('id')
}
$.ajax({
url:'<?php echo base_url;?>messages/readMessage',
type:'GET',
data: form_data,
success:function(d){
alert(d);
}
});
});
But it's not working.
If someone could show me how to do that I'll be thankful.
I changed the ajax code, and it worked I can see now the alert box which is in the success
attribut :
$('.testButton').click(function(){
$.ajax({
url:"<?php echo site_url('messages/readMessage'); ?>",
type:'GET',
data: {id : $(this).data('id')},
success:function(d){
alert(d);
}
});
});
I think the URL after that will be : messages/readMessage?id=myId
, but in CodeIgniter it should be : messages/readMessage/myId
, that's why it doesn't work.
I did a test by giving the id
manually :
url:"<?php echo site_url('messages/readMessage/5'); ?>"
And it worked.
Now I need to know how to concat that value $(this).data('id')
with the url
, because when I tried this : url:"<?php echo site_url('messages/readMessage/" + $(this).data('id') + "'); ?>"
, it didn't worked.
Upvotes: 0
Views: 728
Reputation: 8838
As per your recent edit, try below code in url section
url:"<?php echo site_url('messages/readMessage'); ?>/"+$(this).data('id'),
Then your readMessage
should accept a parameter as below
function readMessage($data ="") {
Upvotes: 1
Reputation: 163
In you controller try by echoing
function readMessage() {
$id=$this->input->get('id');
$this->message_module->readMessage($id);
$this->index();
echo "something";
}
Upvotes: 1