0

I have an area of a page that allows for deleting files. It lists the file names with a delete button next to each. Clicking the button makes an Ajax call to a cfc that deletes the file, and the area with the list reloads with the deleted file gone.

When I first load the page, everything works as it should. Once the area is reloaded, the first item in the list is missing the surrounding form tags and clicking the button will reload the page. Subsequent items have the form tags but will still reload the page. This is my code for the main page:

<div id="uploadedFiles"><cfinclude template="/includes/list-uploaded-files.cfm"></div>

This is the code for list-uploaded-files.cfm:

<cfdirectory action="list" directory="#ExpandPath('/applicant-uploads/' & SESSION.grantID)#" listinfo="name" name="qGetUploads">

<cfif qGetUploads.RecordCount GT 0>
    <table class="table table-hover">
        <cfoutput query="qGetUploads">
            <cfset documentPath = "../applicant-uploads/" & SESSION.grantID & "/" & name>
            <tr>
                <td>
                    <a href="#documentPath#" target="_blank">#name#</a>
                </td>
                <td class="text-right deleteFormContainer">
                    <form>
                        <input name="documentPath" type="hidden" value="#documentPath#" />
                        <button class="btn btn-warning btn-sm submitFileDelete">delete</button>
                    </form>
                </td>
            </tr>
        </cfoutput>
    </table>
    <cfelse>
    <p>No supporting documents have been uploaded.</p>
</cfif>

This is the jQuery for the Ajax:

////////////////////////////////////////////////////////////////////////////////////////////
// DELETE DOCUMENT
////////////////////////////////////////////////////////////////////////////////////////////

$(".submitFileDelete").on("click",function(){
    event.preventDefault();
    var form = $(this).closest("td").find("form");

  var formData = $(form).serialize();
    console.log(formData);
    $.ajax({
        type: "POST",
        url: "/cfc/documents.cfc?method=deleteDocument",
        data: formData,
        dataType: "text",
        beforeSend:function(){
            return confirm("Are you sure you want to delete this file?");
    },
        success: function(data, status) {
            $("#uploadedFiles").load('/includes/list-uploaded-files.cfm');
            console.log(status);
            console.log(data);
        }
    });
});

Is there something about loading a CF page through Ajax that I'm missing? Thanks!

Melissa N.
  • 11
  • 3
  • 1
    `$(".submitFileDelete")` will only work on elements that exist at the time the code runs - as you're loading HTML after this code runs, the click handler won't run and your `.preventDefault` won't run so the page reloads. Use event delegation for this part (not clear if this is or isn't related to other issues you're having) – freedomn-m Aug 25 '20 at 17:54
  • Also, change your ` – freedomn-m Aug 25 '20 at 17:55

0 Answers0