Add items within an array to later save

Asked

Viewed 225 times

0

Hello, I have the following situation and would like a hint if possible how would be the logic to implement this procedure in my application PHP Laravel.

I’m creating a stock system, in the drive part, I have an Input view where I select a product in the combo and click add, every time selecting a product and clicking on the add should stay in memory so that later I can click save and at that time save in the table of drives.

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

I need some help to implement this procedure.

  • There are several forms, it can be for example with AngularJs and then send via ajax, has enough shape.

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":["teste1","teste2","teste3","teste4"]}

<!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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.