I've been trying to update a database row without having to reload a page or redirect to another page to commit the query. I've tried anything I could, but no luck. I have a gallery page. When a user submits an artwork, by default it's not approved and can be approved by an authorized user such as a moderator. The moderator page lists unapproved artworks. The database has a column called approved, its value being either 0 or 1.
Here is my codes.
UPDATE:
Thanks a lot for taking your time. I made the changes you said but it's still not working. Admittedly, I'm not really so good with jQuery/Ajax, so it must be a mistake on my part as to why it didn't work. My codes are now like this:
Model function:
function approve($id)
{
$this->db->where('id', $id);
$this->db->update('galeri', array('approved' => '1'));
return TRUE;
}
Controller
function approve($id)
{
if ($this->galeri->approve($id)) {
echo("VALID");
}
}
HTML
.
.
.
<script type="text/javascript">
$(document).ready(function() {
var test = $("a.approval").attr("id").replace('artid-','');
$.ajax({
type: 'post',
url: 'approve',
data: test,
beforeSend: function() {
parent.animate({'backgroundColor':'#fb6c6c'},300);
},
success: function(data, textStatus, jqXHR) {
if (data == "VALID") {
parent.slideUp(300,function() {
parent.remove();
});
};
}
});
});
});
</script>
.
.
.
<?php foreach ($query->result() as $list): ?>
<span style="display: block">
<?php echo $list->path; ?> -- <a href="javascript:void(0)" id="artid-<?php echo $list->id; ?>'" class="approval">Approve artwork</a>
</span>
<?php endforeach; ?>
.
.
.
echoback a string or JSON object telling your jQuery AJAXwhat's up!approve($id)needs anechoreturn in order for your jquery ajax to do anything based on returned data. That said, your params forsuccessar(data, textStatus, jqXHR), wheredatais the echo returned by php