May 14, 2012

Save multiple form

Question by Kioko Kiaza

Having this Entity for example:

<?php

namespace GitekHotelBundleEntity;

use DoctrineORMMapping as ORM;

/**
 * GitekHotelBundleEntityProduct
 *
 * @ORMTable()
 * @ORMEntity(repositoryClass="GitekHotelBundleEntityProductRepository")
 */
class Product
{
    /**
     * @var integer $id
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string $name
     *
     * @ORMColumn(name="name", type="string", length=100)
     */
    private $name;

How to build a Form to save like 10 products on a row? I want a form with a “+” button and dinamically add line and submit all products in a row.

any help or clue?
thanks in advance

Answer by Farhan Ahmad

You could use Javascript(jQuery) to add the form elements to your page dynamically when you click the “Add” button. Something like this:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
                    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title></title>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>

    <script type="text/javascript">
        $(document).ready(function() {
            $('#btnAdd').click(function() {
                var num     = $('.clonedInput').length;
                var newNum  = new Number(num + 1);

                var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

                newElem.children(':first').attr('id', 'name' + newNum).attr('name', 'name' + newNum);
                $('#input' + num).after(newElem);
                $('#btnDel').attr('disabled','');

                if (newNum == 5)
                    $('#btnAdd').attr('disabled','disabled');
            });

            $('#btnDel').click(function() {
                var num = $('.clonedInput').length;

                $('#input' + num).remove();
                $('#btnAdd').attr('disabled','');

                if (num-1 == 1)
                    $('#btnDel').attr('disabled','disabled');
            });

            $('#btnDel').attr('disabled','disabled');
        });
    </script>
</head>

<body>

<form id="myForm">
    <div id="input1" style="margin-bottom:4px;" class="clonedInput">
        Name: <input type="text" name="name1" id="name1" />
    </div>

    <div>
        <input type="button" id="btnAdd" value="add another name" />
        <input type="button" id="btnDel" value="remove name" />
    </div>
</form>

</body>
</html>

Reference
Then when the form is posted, you can walk through the $_POST array.

Answer by Starx

That is very least amount of code, to see your working process.

But basically such tasks are solved, by storing all the information on the session as a multidimensional array and finally inserting them one by one on the last point.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!