The question is as follows: there is a MySQL database and a PHP page, n number of forms for entering data into the database is output from the database and, accordingly, each form has its submit button (for example, a list of products in which id , name, price and save button).

I want to make one submit button, not n buttons and click every time to save for each element. I think it’s pointless to spread the code - I don’t ask you to do it for me, send in the right channel the thoughts on this matter, that you need to transfer an PHP array to a script that will parse the array and write it down line by line (in the right direction, I think, or is there a more elegant solution?) To give the simplest example on jquery + ajax + php or php , I think it’s easy to change to fit your needs. Thank you in advance.

    2 answers 2

    Initial data set obtained from the database:

     $forms = [ [ 'id' => 1, 'name' => 'Name 1', 'price' => 100 ], [ 'id' => 2, 'name' => 'Name 2', 'price' => 200 ], [ 'id' => 3, 'name' => 'Name 3', 'price' => 300 ] ]; 

    You should display all fields in one form. The name of each field must contain the id of its record.

     <form id="form"> <?php foreach($forms as $form): ?> <div> <input type="hidden" name="forms[<?php echo $form['id']; ?>][id]" value="<?php echo $form['id']; ?>"> <input type="text" name="forms[<?php echo $form['id']; ?>][name]" value="<?php echo $form['name']; ?>"> <input type="text" name="forms[<?php echo $form['id']; ?>][price]" value="<?php echo $form['price']; ?>"> </div> <?php endforeach; ?> <input type="submit" value="Submit"> </form> 

    Thus, when sabmite formed an array similar to the original.

     <script src="https://code.jquery.com/jquery-2.2.3.min.js"></script> <script> $(function() { $('#form').on('submit', function(e) { e.preventDefault(); $.post('post.php', $(this).serializeArray(), function(response) { console.log('success'); }); }); }); </script> 

    And finally, a script that updates the data in the database.

     $forms = $_REQUEST['forms']; foreach ($forms as $form) { // UPDATE table SET name = $form[name], ... WHERE id = $form[id] } 
    • Thank! Everything is clear and to the point. Today I will try to implement =)) +1 - Konstantin

    If you need to display several (goods) directly - I think the most optimal way is a single form with one button, an array in php, and let them figure out what to save and where.
    If you don’t need a few at once, issue them in a chain (one product after another) and save in turn
    Well, or a combination of both - give out a pack, after a change - automatically save via ajax, remove from the page (and substitute a new one).

    • In principle, it is clear, I also thought in the direction of a single form and transmission of an array, the question is how to transfer this array or is it more correct to say? I mean there are n number of inputs in one big form, with the generated PCPs, so these intupts will have the same name or I don’t understand something in a cycle they can be given different names for example. The question is how to process this form? Write a thom to a variable and pass it to the script or transfer to the script somehow differently? <form> <input1> <input2> <input3> <input1> <input2> <input3> <submit> </ form> - Constantine