I added an icon next to a file that, when clicked, will mark the file as a redline and change the color of the text of the filename. My boss wants me to do this via AJAX, but I've never done AJAX before.
I copied some other code and modified it, but I keep getting an error and I'm not sure why.
This is the code:
echo '<img src="' . site_url('img/red-flag.png') . '" height="15px" title="Mark as redline" onclick="markAsRedline(' . $attachment['id'] . ');">';
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/mootools/1.4.5/mootools-yui-compressed.js"></script>
<script type="text/javascript">
function markAsRedline(attachmentid) {
new Request.JSON({
url: '<?php echo site_url('MAttachment/markAsRedline'); ?>/' + attachmentid,
onSuccess:
alert('Success')
}).send();
}
</script>
What the PHP function (MAttachment/markAsRedline) does is mark a field in a table as 1. When the page loads, if the field is 1, the text will display in a different color.
The error I'm getting after I see the alert is:
POST http://localhost/###/MAttachment/markAsRedline/1744578 404 (Not Found)
The text is not changing color either. I have no clue what is going wrong. Any help would be very much appreciated.
I found out that I can't call a function in a model function (MAttachment), I have to create a controller function to call the model function and then call the controller function. I did that and it all works.