How to add key/value pair to existing form data

Updated at 24-Sep-2021, By samar

How to add key/value pair to existing form data

Through the use of the programming language, we will work together to solve the "How to add key/value pair to existing form data" puzzle in this lesson.

You can add key/value to an existing form data while calling the ajax request. Sometimes we have to add extra data to a form data which does not exist in form input elements then we can add this data to existing form data.
  • Add key/value pair to existing form data

    <script> 
    var id = $('#recordID').val();
    formData.append('id', id);
    //Code snippet with form data
    
    $('#form').on('submit', function(e){
        e.preventDefault();
        var formData = new FormData($(this)[0]);
        var id = $('#recordID').val();
        formData.append('id', id);
    
        $.ajax({
            type: "POST",
            dataType: "json",
            url: "/add-record",
            data: formData,
            processData: false,
            success: function(data){
                console.log(data);
            }
        }); 
    })
    </script>
    

    You have to use formData.append('id', id); to add a new key/value pair to existing form data.

Back to code snippet queries related javascript