Se ho capito bene, vuoi scegliere elementi da una lista nella tabella A, farli comparire nella tabella B e poi fare clic su un pulsante di submit per salvare gli elementi scelti. Ecco una soluzione.
Nomina questo file test.php:
<?php /** * Created by PhpStorm. * User: SQ05 * Date: 06/01/2017 * Time: 06:31 PM */ // Note: $data below is a typical example of data fetched from the DB. Push each // $row into an array so you build something like: // $data = array(); // $i = 0; // while ($row = mysql_fetch_assoc($query)) { $data[$i] = $row; $i++; } // That produces the structure shown below. $data = array( array('id' => 1, 'name' => 'Loveth', 'age' => 23, 'phone' => '9889087455'), array('id' => 2, 'name' => 'Susan', 'age' => 51, 'phone' => '787987455'), array('id' => 3, 'name' => 'Precious', 'age' => 13, 'phone' => '98989087455'), array('id' => 4, 'name' => 'Hannah', 'age' => 21, 'phone' => '987087455'), array('id' => 5, 'name' => 'Kenneth', 'age' => 43, 'phone' => '569087455'), ); ?> <div style="float:left;width:50%"> THE LIST OF ALL ITEMS <table> <tr><td>Name</td><td>Age</td><td>Phone</td><td>Action</td></tr> <?php for ($i = 0; $i < count($data); $i++) { $item = $data[$i]; ?> <tr> <td><?php echo $item['name'];?></td> <td><?php echo $item['age'];?></td> <td><?php echo $item['phone'];?></td> <td><button onclick='addToList(<?php echo json_encode($item);?>);'>Add</button></td> </tr> <?php } ?> </table> </div> <div style="float:left; width:50%"> THE ITEMS TO BE SAVED <div id="showToSave"></div> </div> <script src="bower_components/jquery/dist/jquery.js"></script> <!-- Please install jquery based on your own directory path --> <script> var listToSave = []; // must be global /** Process data and add an item to the list. */ var addToList = function (data) { var lenData = listToSave.length; if (lenData > 0) { // avoid duplicates for (var j = 0; j < lenData; j++) { if (data.id == listToSave[j].id) return; } } listToSave.push(data); console.log(listToSave); document.getElementById('showToSave').innerHTML = createData(listToSave); }; var createData = function (data) { var len = data.length; var tableToSave = "<table><tr><td>Name</td><td>Age</td><td>Phone</td><td>Action</td></tr>"; for (var i = 0; i < len; i++) { var content = data[i]; tableToSave += "<tr><td>" + content.name + "</td><td>" + content.age + "</td><td>" + content.phone + "</td><td><button onclick='deleteFromSave(" + i + ")'>Delete</button></td></tr>"; } tableToSave += "</table><div><button onclick='saveData()' type='button'>Save</button></div>"; return tableToSave; }; /** Remove an item that was added locally. */ var deleteFromSave = function (index) { listToSave.splice(index, 1); document.getElementById('showToSave').innerHTML = createData(listToSave); }; /** Submit the collected data. */ var saveData = function () { $.ajax({ type: "POST", url: "getData.php", data: { list: JSON.stringify(listToSave) }, success: function (resp) { console.log('response=', resp); } }); }; </script> Nomina questo file successivo getData.php per elaborare la lista inviata usando un approccio di inserimento a valori multipli:
<?php /** * Created by PhpStorm. * User: SQ05 * Date: 06/01/2017 * Time: 07:28 PM */ if ($_SERVER['REQUEST_METHOD'] == 'POST') { $data = json_decode(stripslashes($_POST['list'])); // $data can now be used to build a multiple-value insert into your MySQL DB. print_r(json_encode($data)); // send a response back to the page (echo if string) }