The JavaScript-generated form element will not be included in the data submitted through the POST method

While it may appear that this question has been asked before, my specific inquiry seems to be unique as I have not found a similar answer in other threads. Therefore, I am initiating a new discussion.

My issue revolves around a form which contains a link that, when clicked, adds HTML content to a div within the form. Users have the ability to add this content multiple times. However, upon submitting the form, only the field added for the first time using JavaScript gets posted through the form. The subsequent data from the second and following forms is not submitted. I have included my code below and would greatly appreciate any assistance in identifying and resolving this issue.

<form action="<?php echo $this->getUrl('children/index/addChild') ?>" method="post" id="form-validate" autocomplete="off">

    <div class="fieldset">
        <?php //echo $this->getBlockHtml('formkey')?>
        <h2 class="legend"><?php echo $this->__('Child Name') ?></h2>
        <?php if(sizeof($model)==0): ?>



        <a href="javascript:show()" id="addchild">Add Child</a>

    <div id="child" style="display:none;">
        <li>
            <div class="field">
                    <label for="firstname" class="required"><em>*</em><?php echo $this->__('First Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="firstname" id="firstname" value="<?php  ?>" title="<?php echo $this->__('First Name') ?>" class="input-text required-entry" />
                    </div>
            </div>
            <div class="field">
                    <label for="lastname" class="required"><em>*</em><?php echo $this->__('Last Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="lastname" id="lastname" value="<?php  ?>" title="<?php echo $this->__('Last Name') ?>" class="input-text required-entry" />
                    </div>
            </div>        
        </li>
        <li>
            <div class="field">
                    <label for="schoolname" class="required"><em>*</em><?php echo $this->__('School Name') ?></label>
                    <div class="input-box">

                        <select id='schoolname' name='schoolname'>
                            <option value="0">Select School</option>
                            <?php foreach ($schools as $key => $school): ?>
                                <option value="<?php echo $school['school_id']; ?>"><?php echo $school['school_name'] ?></option>
                            <?php endforeach;?>


                        </select>
                    </div>
            </div>  
            <div class="field">
                    <label for="year" class="required"><em>*</em><?php echo $this->__('Year') ?></label>
                    <div class="input-box">
                        <input type="text" name="year" id="year" value="<?php  ?>" title="<?php echo $this->__('Year') ?>" class="input-text required-entry" />
                    </div>
            </div>
            <div class="field">
                    <label for="class"><?php echo $this->__('Class(if applicable)') ?></label>
                    <div class="input-box">
                        <input type="text" name="class" id="class" value="<?php  ?>" title="<?php echo $this->__('Class') ?>" class="input-text" />
                    </div>
            </div>
        </li>
        <button type="submit" title="<?php echo $this->__('Save') ?>" class="button"><span><span><?php echo $this->__('Save') ?></span></span></button>
    </div>
<?php else: ?>
<?php $i=1;
foreach ($model as $key => $value): ?>
    <div id="child">
        <?php 
        $arr = array(); 
        $id= $value['child_id']; 

        ?>
        <a href="javascript:remove('<?php echo $id; ?>')" id="removechild">Remove Child</a>
        <li>
            <input type="hidden" value="<?php echo $id; ?>" name="childid<?php echo $i ?>" />
            <div class="field">
                    <label for="firstname<?php echo $i ?>" class="required"><em>*</em><?php echo $this->__('First Name') ?></label>
                    <div class="input-box">
                        <input type="text" name="firstname<?php echo $i ?>" id="firstname<?php echo $i ?>" value="<?php echo $value["firstname"]; ?>" title="<?php echo $this->__("First Name") ?>" class="input-text required-entry" />
                    </div>
            </div>
            <!-- Additional Fields -->

        <button type="submit" title="<?php echo $this->__('Save') ?>" class="button"><span><span><?php echo $this->__('Save') ?></span></span></button>
<?php endif; ?>
</form>

and here is the accompanying script:

function duplicate(){

    var id=document.getElementById('test').value;

    var div = document.createElement('div');

    div.className = "row"+id;

    // Additional fields to be appended dynamically

     document.getElementById('addition').appendChild(div);
     document.getElementById('test').value= ++id;

}

If you can provide any insight or suggestions on how to address this issue, I would greatly appreciate it. Thank you!

Answer №1

Make sure to check out this jsfiddle, where you can find a submit handler that serializes data and sends it to the server.

$('#form-validate').on('submit', function (e) {
    //stop the default form submission
    e.preventDefault();
    alert($(this).serialize())
    //send the form data to 'yourURL'
    $.post('yourSubmitUrl.php', $(this).serialize());
});

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

onpageshow event behaves as expected exclusively on webkit browsers (triggers function solely when visible on the screen)

I am inserting an HTML file using an object tag. The encompassing div of the object tag is hidden with a display:none property. However, I encounter an issue where the onpageshow event that I placed on the body tag of the HTML object behaves differently a ...

Implementing a loop in jQuery is not functioning as expected

Can someone help me figure out how to use a for loop to create a jQuery function? I'm having trouble when adding the array parameter in the string, it just doesn't seem to work correctly. Any suggestions? var ids=["width_uncheck","thickness_unch ...

Organize and display a list of contacts alphabetically by the first letter of their

I have a list of contacts that I need help with. Despite searching on Stack Overflow, I couldn't find the answer. Can someone please assist? Thank you. export const rows = [ { id: 1, name: 'Snow', email: 'Jon', co ...

arrange objects into an array depending on punctuation marks

Currently, I am in the process of developing a method to retrieve data from my MySQL database and present it as an array. However, the issue I am facing is that only one string of data is accessible at a time, with each instance separated by a comma. For e ...

What is the process for incorporating Express.js variables into SQL queries?

Recently delving into the world of node.js, I've embarked on a journey to create a login and registration system using express.js, vanilla JS, CSS, HTML, and MySql. Within the following code lies the logic for routing and handling HTTP Post requests ...

When you try to remove an element from an array by its index in Vue JS, the outcome may not be

Here is an array that I have: var hdr = ("name", "date", "start_time", "selling_item", "total_call", "end_time", "ad_num", "area", "order_num"); //this data comes from the database Now, I need to rename these array elements according to prope ...

The try and catch block in JavaScript is failing to correctly capture the HTTP status

I have a function that successfully posts JSON to an API endpoint. Here is the code I am using: function sendValuesPageLoad(){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { try { if (xhr.readyState === ...

Incorporate row numbering feature into jQuery DataTables

Is there a way to have jQuery datatables generate a row number column in the first column, similar to a datagrid in VB? This is what I'm aiming for: If you have any insights on how this can be achieved, please share! ...

looking to display the latest status value in a separate component

I am interested in monitoring when a mutation is called and updates a status. I have created a component that displays the count of database table rows when an API call is made. Below is the store I have implemented: const state = { opportunity: "" } ...

Ways to eliminate all attributes and their corresponding values within HTML tags

Hey there, I'm trying to strip away all the attribute values and styles from a tag in html Here's my Input: <div id="content"> <span id="span" data-span="a" aria-describedby="span">span</span> <p class="a b c" style=" ...

Incorporating JavaScript into a Rails Application

When I click on a link "link_to" in my rails app, I am attempting to trigger a JS file. To confirm that the asset pipeline is functioning correctly, I conducted a test with: $(document).ready(function() { alert("test"); }); The alert successfully po ...

Unusual layout in Next.js editor (VS Code)

My chosen formatter is prettier, but I'm encountering an issue with the way it handles simple JSX functions. Initially, my code looks like this: function HomePage() { return; <div> <h1>Hello Next.js</h1> <p> Welcome ...

Terminate the ongoing PHP script execution

Is there a way to terminate the current PHP script? For instance: page.php <?php require('./other-page.php'); ?> <h4>this text is always displayed</h4> other-page.php <?php if($_GET['v'] == 'yes&ap ...

Looking for a custom JavaScript color wheel with advanced features?

In search of a javascript color picker/wheel that allows users to easily select colors for our paint shop. Once the color is selected, it should automatically add the color value to the shopping cart. Our online store is operated using PrestaShop, so we ...

How can I activate a function or pass a selected value to a different scope variable using AngularUI Bootstrap Datepicker?

Check out this AngularUI Datepicker demo on Plunker: http://plnkr.co/edit/DWqgfTvM5QaO5Hs5dHco?p=preview I'm curious about how to store the selected value in a variable or trigger another function when a date is chosen in the field. I couldn't ...

What is the best way to set the v-model property to an object that is constantly changing

I'm in the process of creating a dynamic form that allows users to add additional fields by simply clicking on a button labeled "adicionar condição." The concept is similar to what can be seen in the screenshot below: https://i.stack.imgur.com/Mpmr6 ...

Can a browser still execute AJAX even if the window.location is altered right away?

Here is the situation I am facing: <script> jQuery.ajax{ url : 'some serverside bookkeeping handler', type : post, data : ajaxData }; window.location = 'Some new URL'; </script> Scenario: I n ...

Internet Explorer versions 9 and 10 do not support the CSS property "pointer-events: none"

In Firefox, the CSS property pointer-events: none; functions properly. However, it does not work in Internet Explorer 9-10. Are there any alternative approaches to replicate the functionality of this property in IE? Any suggestions? ...

What is the best way to pass an array in a .ajax request using jQuery?

After cycling through a simple process of collecting data and adding it to an array, I encountered an issue when trying to send that array to a page method (.aspx). It seems like there is something in the array causing a problem. Here is the snippet of m ...

obtain information from a Java servlet on a web page

I am working on a webpage that displays various coordinates (longitude, latitude), and I need to create a Java class to sort these coordinates. My plan is to send the coordinates to a Java servlet before displaying them on the webpage. The servlet will th ...