Add items within an array to save later

0

Hello, I have the following situation and would like a tip if possible as would be the logic for implementing this procedure in my Laravel PHP application.

I'm creating a stock system, in the moving part, I have an Input view where I select in the combo a product and click on add, every time selecting a product and clicking the add should stay in memory so that later I can click save and then save to the move table.

Should I do this using ajax or is there any other way?

I need some help in implementing this procedure.

    
asked by anonymous 23.03.2017 / 21:25

1 answer

0

An interesting solution is to use JSON. In this example you would use a form and pass the parameters to PHP via AJAX. It is only necessary to add the fields inside the form with the same name. This would be the return: {"name": ["test1", "test2", "test3", "test4"]}

<!DOCTYPE HTML>

                           

<body>
    <form id='formulario'>
        <input name='nome' value='teste1'>
        <input name='nome' value='teste2'>
        <input name='nome' value='teste3'>
        <input name='nome' value='teste4'>
    </form>

    <script>
    $.fn.serializeObject = function()
    {
        var o = {};
        var a = this.serializeArray();
        $.each(a, function() {
            if (o[this.name] !== undefined) {
                if (!o[this.name].push) {
                    o[this.name] = [o[this.name]];
                }
                o[this.name].push(this.value || '');
            } else {
                o[this.name] = this.value || '';
            }
        });
        return o;
    };


    $('document').ready(function(){
        json = JSON.stringify($("#formulario").serializeObject());
        $('body').html(json);
    });
    </script>
</body>

    
25.03.2017 / 16:28