Form submission failure due to dynamic input manipulation

I am encountering an issue with a dynamic form that allows users to add and remove inputs. Although the form functions correctly visually, the values from inputs created by the JavaScript function are not being retrieved when the form is submitted. If more information is required, please let me know as this is one of the final additions to the website.

Could anyone suggest why this might be happening? My code is provided below:

The HTML:

<div class="form-container" id="form-container">
                        <div class="title-title">
                            <h3 class="recipe-pretitle">Recipe for</h3>
                            <form action="/recipes/create" enctype="multipart/form-data" method="POST">
                                <div>
                                    <label for="title">
                                        <input name="title" type="text" id="title" placeholder="Name of dish">
                                    </label>
                                </div>
                        </div>

                        <div class="description-container">
                            <label class="description-label" for="description">A brief description of your dish: <br> (max 80char)</label>
                            <textarea name="description" type="text" id="description" cols="30" rows="5"></textarea>
                        </div>

                        <div class="directions-ingredients-container">

                            <div id="product-directions">
                                <div class="label-directions"><label for="directions">Cooking steps.</label></div>
                                <div class="controls-ol-container">
                                    <div class="controls">
                                        <a href="#" id="add_more_fields"><i class="fa fa-sm">Add step</i></a>
                                        <a href="#" id="remove_fields"><i class="fa fa-sm">Remove last step</i></a>
                                    </div>
                                    <div class="instruction-list-container">
                                        <ol id="instruction-list">
                                        </ol>
                                    </div>
                                </div>
                            </div>

                            <div class="ingredients-container">
                                <div class="label-ingredients"><label for="Ingredients">Ingredients:</label></div>
                                <div class="controls-ol-container">
                                    <div class="controls">
                                        <a href="#" id="add_more_fields_ingredients"><i class="fa fa-sm">Add Ingredient</i></a>
                                        <a href="#" id="remove_fields_ingredients"><i class="fa fa-sm">Remove last Ingredient</i></a>
                                    </div>
                                    <div class="ingredient-list-container">
                                        <ol id="ingredient-list">
                                        </ol>
                                    </div>
                                </div>
                            </div>

                        </div>

                        <div class="imageInputContainer">
                            <label id="image-label" for="image">Choose an image</label>
                            <input name="image" type="file" id="image">
                        </div>
                        <div>
                            <button type="submit">Upload</button>
                            </form>
                        </div>
                    </div>

Frontend JS:

 var add_more_fields = document.getElementById("add_more_fields")
    var remove_fields = document.getElementById("remove_fields")
    var directions_container = document.getElementById('product-directions')
    var instruction_list = document.getElementById('instruction-list')



    add_more_fields.onclick = function () {
        var node = document.createElement("li")
        var newField = document.createElement('input')
        newField.setAttribute('type', 'text')
        newField.setAttribute('name', 'directions[]')
        newField.setAttribute('placeholder', 'Add Instruction')
        node.appendChild(newField)

        instruction_list.appendChild(node)
    }

    remove_fields.onclick = function () {
        var input_tags = instruction_list.getElementsByTagName('li')
        if (input_tags.length > 1) {
            instruction_list.removeChild(input_tags[(input_tags.length) - 1])
        }
    }



    var add_more_fields_ingredients = document.getElementById("add_more_fields_ingredients")
    var remove_fields_ingredients = document.getElementById("remove_fields_ingredients")
    var ingredient_list = document.getElementById('ingredient-list')
    



    add_more_fields_ingredients.onclick = function () {
        var node = document.createElement("li")
        var newField = document.createElement('input')
        newField.setAttribute('type', 'text')
        newField.setAttribute('name', 'Ingredients[]')
        newField.setAttribute('placeholder', 'Add Ingredient')
        node.appendChild(newField)
        ingredient_list.appendChild(node)
    }

    remove_fields_ingredients.onclick = function () {
        var input_tags = ingredient_list.getElementsByTagName('li')
        if (input_tags.length > 1) {
            ingredient_list.removeChild(input_tags[(input_tags.length) - 1])
        }

    }

Answer №1

Your HTML is not valid because the form element is not properly closed. To fix this, move the closing form tag outside the button container and bring the form opening tag up a level.

Now that you have made these adjustments, be sure to modify styles and rearrange elements as needed to ensure the form and HTML are valid:

<div class="form-container" id="form-container">
    <form action="/recipes/create" enctype="multipart/form-data" method="POST">
        <div class="title-title">
            <h3 class="recipe-pretitle">Recipe for</h3>
            <div>
                <label for="title">
                    <input name="title" type="text" id="title" placeholder="Name of dish">
                </label>
            </div>
        </div>
        <div class="description-container">
            <label class="description-label" for="description">A brief description of your dish:
                <br> (max 80char)</label>
            <textarea name="description" type="text" id="description" cols="30" rows="5"></textarea>
        </div>
        <div class="directions-ingredients-container">
            <div id="product-directions">
                <div class="label-directions">
                    <label for="directions">Cooking steps.</label>
                </div>
                <div class="controls-ol-container">
                    <div class="controls">
                        <a href="#" id="add_more_fields"><i class="fa fa-sm">Add step</i></a>
                        <a href="#" id="remove_fields"><i class="fa fa-sm">Remove last step</i></a>
                    </div>
                    <div class="instruction-list-container">
                        <ol id="instruction-list">
                        </ol>
                    </div>
                </div>
            </div>
            <div class="ingredients-container">
                <div class="label-ingredients">
                    <label for="Ingredients">Ingredients:</label>
                </div>
                <div class="controls-ol-container">
                    <div class="controls">
                        <a href="#" id="add_more_fields_ingredients"><i class="fa fa-sm">Add Ingredient</i></a>
                        <a href="#" id="remove_fields_ingredients"><i class="fa fa-sm">Remove last Ingredient</i></a>
                    </div>
                    <div class="ingredient-list-container">
                        <ol id="ingredient-list">
                        </ol>
                    </div>
                </div>
            </div>
        </div>
        <div class="imageInputContainer">
            <label id="image-label" for="image">Choose an image</label>
            <input name="image" type="file" id="image">
        </div>
        <div>
            <button type="submit">Upload</button>
        </div>
    </form>
</div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Fetching information from external API prior to displaying it

I am currently working on an application that is built using the React Starter Kit. Each page within the app includes a fetch function that retrieves data from an API during the componentDidMount lifecycle. My goal is to retrieve the data before renderin ...

Is it feasible to utilize a prop for styling in Vue using SCSS/SASS?

I am currently working on enabling custom theming for my component that utilizes bootstrap. I am aiming to define its $primary tag in SCSS within the section of the Vue component. Currently, my styling setup looks like this: <style scoped lang="scss"& ...

Generate a new file using the data from an AJAX request

Looking for a PHP script that can generate a file from the content of an AJAX post and name it report.txt. The following PHP script is located at /var/www/copypaste.test/public_html/index.php <?php header('Access-Control-Allow-Origin: *'); ...

When attempting to remove a single docker container, several containers were mistakenly deleted in the

I am currently working on a project that involves distributed nodes within docker containers on a docker network. Specifically, I have nodes 1, 3, 4, 5, and 7 in total. To manage these nodes, I use a CLI process on my local machine. For instance, I can e ...

Incorporate a stylish gradient background into react-chartjs-2

I am currently working on adding a gradient background with some transparency to a radar graph within a react component. So far, I have only found solutions that involve referencing a chartjs canvas in an html file, but none for implementing it directly in ...

Applying 100% height to a div element for positioning

I would like to achieve the following layout: The navigation should be fixed at the top, and the height of the div with the background image should be 100%. I want to place content below the image. My issue is that when I set the positioning of the image ...

Having trouble with sending a JSON post request in Flask?

I have a setup where I am utilizing React to send form data to a Flask backend in JSON format. Here is an example of the code: add_new_user(e){ e.preventDefault() var user_details = {} user_details['fname'] = this.state.first_name user_d ...

"Unlocking the Power of Facebook with Javascript and Ajax

My Cordova app successfully authenticates with Facebook, but when trying to retrieve data, I encounter errors. I suspect there may be an issue with my URL. Can anyone identify a mistake in this? Errors: app: makeAPICalls: error:setting authenticatedUser ...

Deciphering Parameters from a URL using Angular

My goal is to track specific metrics in Google Analytics by extracting certain parameters, removing sensitive information, and sending them off as a comma-separated string. For example: this=me&that=you should be transmitted to GA as: this,that I h ...

Storybook encounters an undefined error with the Vuex store

Here is a snippet from my main.js file: import './plugins'; import store from './store'; import FileUpload from './components/FileUpload'; export default { install(Vue) { Vue.component('file-upload', ...

Position the Material-UI AppBar and Tab on the far right of the screen

I am trying to achieve a specific layout where the Links for "Homepage Login Settings and etc.." are placed at the right edge of the AppBar, while keeping the "Website" aligned to the left edge of the screen. Here is what I have: https://i.sstatic.net/Wvo ...

Can Bootstrap support breaking the inline code block?

I am currently developing Blaze, an application that utilizes the SE API to retrieve recent content. It is effective in identifying NAAs, but a particular issue arises when someone places extensive code or even an entire sentence within an inline code bloc ...

The watch functionality is failing to work within a specialized attribute directive that is being utilized within a separate custom element directive

I'm currently working with two directives that handle separate functionalities. I am attempting to utilize the attribute directive within an element directive, but I'm encountering issues with the watch function not working on the attribute direc ...

Using Node.js with Express and MySQL to render a view after a post request

I have successfully implemented an insert and list function using Express with a MySQL database. The issue I am facing is how to automatically render the list after an insert operation. Do I need to call the select statement again manually? var mysql ...

Verification of radio selections

I have successfully implemented validation for the radio button. Now, I would like to add an alert box that asks "Are you sure you want to pick 'x'?" before proceeding with the code. If the user clicks cancel, they should return to the webpage. B ...

I'm having trouble loading gltf files in Three.js GLTFLoader on my computer

Recently, I began exploring the world of three.js, JavaScript, HTML, and CSS as I delve into the process of loading a 3D model. So far, I've had success when retrieving a gltf file from an online source, like this: loader.load('https://threejsfun ...

The proper way to send an email following an API request

I am currently developing an express API using node.js, and I want to implement a feature where an email is sent after a user creates an account. I have tried various methods, but none of them seem to be the perfect fit for my requirements. Here is some ps ...

Utilizing Selenium to Transmit Text in Alert Messages

What is the best way to deal with a JavaScript alert that includes text fields like username and password? How can you input values in those fields? https://i.sstatic.net/AXC5c.png ...

Animate the height transition of contenteditable after a line of text is added (using shift+enter) or removed

Currently, the contenteditable attribute is being utilized on the <div> tag to enable autogrow functionality similar to a textbox. Additionally, there is an attempt to incorporate a height transition. While most aspects are functioning correctly, the ...

Error JSON material for MeshFaceMaterial encountered

Successfully loaded my model using the following code: loader.load( "js/charWalk01.js", function( geometry, materials ) { mesh = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial() ); scene.add( mesh ); } ...