Create multidimensional array based on recursive HTML-form
Hi, I am working on an application with some workout exercise. For this I am building a HTML-form which is repeated several times as there are different exercises which needs to be entered.
I want to transfer the input into a multidimensional array I can work in PHP before inserting into a database. I found a way through serialize(), but it lists all input out as individual arrays. I can manage this data and set up a function to sort them later in PHP, but I believe there is a better way to structure the data in a multidimensional array before even sending to PHP. I am looking for an array which loos sorta like
- array(
- [exercise_id_1] => array(
- [set_1] => array (
- key1 => item 1,
- key2 => item 2,
- ....
- )
- [set_2] => array (
- key1 => item 1,
- key2 => item 2,
- ....
- )
- )
- [exercise_id_2] => array(
- [set_1] => array (
- key1 => item 1,
- key2 => item 2,
- ....
- )
- [set_2] => array (
- key1 => item 1,
- key2 => item 2,
- ....
- )
- )
My PHP looks like
- echo "<form method=\"post\" name=\"save_workout_form\" class=\"save_workout_form\">";
- foreach ($array as $key=>$value) {
- echo "\n<div class=\"view_workout\">\n<legend>".$array[$key][0]['Exercise_Name']."</legend>";
- echo "<table>";
- echo "<tr>";
- echo "<td></td><td>Weight</td><td>Reps</td>";
- echo "</tr>";
- for ($j=0; $j < count($array[$key]); $j++) {
- echo "<tr>";
- echo "<td><div class=\"view_workout_set_id\"># ".$array[$key][$j]['set_ID']."</div></td>\n";
- echo "<td><input type=\"input\" name=\"Weight\" placeholder=\"".$array[$key][$j]['Weight']."\"</td>\n";
- echo "<td><input type=\"input\" name=\"repetition\" placeholder=\"".$array[$key][$j]['repetition']."\"</td>\n";
- echo "<input type=\"hidden\" name=\"field_id\" value=\"h\">\n";
- echo "<input type=\"hidden\" name=\"exercise_id\" value=\"h\">\n";
- echo "<input type=\"hidden\" name=\"planned_workout_id\" value=\"h\">\n";
- echo "</tr>";
- }
- echo "\n</table>";
- echo "\n<input type=\"submit\" value=\"Save\" class=\"save_workout_btn\">";
- echo "\n</div>";
- }
- echo "</form>";
my jQuery is:
- $( document ).ready(function(){
- // $(function() {
- $(document).on('click', ".save_workout_btn", function(event){
- event.preventDefault();
- console.log("Save workout btn er trykket");
- $('.save_workout_form').submit();
- });
- });
- //IF "SAVE WORKOUT" BTN IS CLICKED PERFORM DATA TRANSFER TO PHP FILE
- $( document ).ready(function(){
- $(function() {
- $(document).on('submit', ".save_workout_form", function(event){
- console.log("save workout function is activated");
- //var dataInput = $('input[name=Weight]').serializeArray()
- var dataInput = $(".save_workout_form").serializeArray();
- var workout_id_array = { "id": 1, "class": 'save_workout',"dataArray": dataInput};
- $.ajax({
- type : 'POST',
- url : 'action_page.php',
- data : workout_id_array,
- success : function (data) {
- console.log(data);
- event.preventDefault();
- } // closing succss
- }); // closing $.ajax({
- event.preventDefault();
- }); //closing $(document).on('submit', ".save_workout_form", function(event){
- });
- });
The output is:
- [dataArray] => Array
- (
- [0] => Array
- (
- [name] => Weight
- [value] =>
- )
- [1] => Array
- (
- [name] => repetition
- [value] =>
- )
- [2] => Array
- (
- [name] => field_id
- [value] => h
- )
- ...... looping through all 53 fields
There has got to be a smarter way..