CodeIgniter Forums
resolve ajax failure in codeigniter 3 view page? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: CodeIgniter 3.x (https://forum.codeigniter.com/forumdisplay.php?fid=17)
+--- Thread: resolve ajax failure in codeigniter 3 view page? (/showthread.php?tid=91252)



resolve ajax failure in codeigniter 3 view page? - solmazbabakan - 07-09-2024

in codeigniter 3 view page i have following to edit comments :

PHP Code:
function toggleEdit(commentId) {
    
    console
.log("Toggling edit for comment ID:"commentId);
    var commentText document.getElementById('comment-' commentId);
    var editTextarea document.getElementById('edit-comment-' commentId);
    var button document.getElementById('edit-save-button-' commentId);

    if (editTextarea.style.display === 'none') {
        // Switch to edit mode
        console.log("Switching to edit mode");
        commentText.style.display 'none';
        editTextarea.style.display 'block';
        button.textContent 'Save';
    } else {
        // Save the edited comment
        console.log("Saving edited comment");
        var editedComment editTextarea.value;
        console.log(" edited comment is:",editTextarea.value);
        console.log(" controller method:",'<?=$user_base_url?>/profil/save_comment');

        $.ajax({

            url'<?=$user_base_url?>/profil/save_comment',//+ new Date().getTime(),
            type'POST',
            data: {
                idcommentId,
                commenteditedComment
            
},
            success: function(response) {
                console.log("AJAX call successful");
                console.log(response);
                if (response.success) {
                    console.log("Comment saved successfully");
                    // Update the displayed comment and switch back to view mode
                    commentText.innerHTML editedComment.replace(/\n/g'<br>');
                    commentText.style.display 'block';
                    editTextarea.style.display 'none';
                    button.textContent 'Edit';
                } else {
                    console.error("Error saving comment: "response);
                    alert('Error saving comment.');
                }
            },
            error: function(xhrstatuserror) {
                console.error("AJAX call failed:"statuserror);
                alert('AJAX call failed.');
            }
        });
    }

here i want to add a part that users are adding their comments for page content with ability to edit the comments.

but ajax failed to activated while get_csrf_token/?_=1720512009436 is retrived successfully when i check.

how should i resolve that?


RE: resolve ajax failure in codeigniter 3 view page? - demyr - 07-09-2024

Well, not sure about CI3 but with CI4 I would add :

Code:
<input type="hidden" class="txt_csrfname" name="<?= csrf_token() ?>" value="<?= csrf_hash() ?>" />



Code:
$(document).ready(function(){
        var csrfName = $('.txt_csrfname').attr('name'); // CSRF Token name
        var csrfHash = $('.txt_csrfname').val(); // CSRF hash
...


$.ajax({
            url : "your-url",
            method: "POST",
            data : {name:name, age:age, [csrfName]: csrfHash},
            success:  ...



RE: resolve ajax failure in codeigniter 3 view page? - InsiteFX - 07-09-2024

Correct way to re-generate CSRF for sequential AJAX calls