Jump to content

Recommended Posts

Hi, I am trying to organise some data from a form which is transferred to me via jQuery. Right now I can transfer the data to my php function as either serialised data. Here I need to figure out to structure my data in arrays that allows me to work with them before I save to the database.. But how do I do this? I have been fiddling with explode, and trying to flatten it  and do an array_push using a "while" for every 6'th item..

What I am looking for is this:

Array[$i] (
Weight=>,
repetition=>,
field_id=>,
set_id=>
exercise_id=>,
planned_workout_id=
)

Array[$i++] (
Weight=>,
repetition=>,
field_id=>,
set_id=>
exercise_id=>,
planned_workout_id=
)

The structure I can have the data in

Serialised form:

Weight=&repetition=&field_id=h&set_id=1&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=4&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=1&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=13

or in array form.. which is 200+ subarrays

Array
(
    [0] => Array
        (
            [name] => Weight
			[value] => 
        )

    [1] => Array
        (
            [name] => repetition
            [value] => 
        )

    [2] => Array
        (
            [name] => field_id
            [value] => h
        )

    [3] => Array
        (
            [name] => set_id
            [value] => 1
        )

    [4] => Array
        (
            [name] => exercise_id
            [value] => 1
        )

    [5] => Array
        (
            [name] => planned_workout_id
            [value] => 1
        )

    [6] => Array
        (
            [name] => Weight
            [value] => 
        )

    [7] => Array
        (
            [name] => repetition
            [value] => 
        )

 

Hi Brand,

The data is a dynamic created form, which is contains a number of input-fields which are dublicated X amount of times, so that the user can enter the progress. It is transferred to my PHP page via jQuery, using the serialise function. The raw data which is transferred can be delivered in 2 different formats, whichever I choose - See the snips below.

How I save the data in my database is going to be multiple different tables,I want to normalise my data as much as possible when I store it. Furthermore the data will be enriched by other data sources before I save it the database.

Serialised form:

Weight=&repetition=&field_id=h&set_id=1&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=4&exercise_id=1&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=1&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=2&exercise_id=13&planned_workout_id=1&Weight=&repetition=&field_id=h&set_id=3&exercise_id=13

or in array form.. which is 200+ subarrays

Array (    
	[0] => Array (           
		[name] => Weight
		[value] =>         
	)     
	[1] => Array (             
		[name] => repetition             
		[value] =>         
	)
	[2] => Array (           
		[name] => exercise_id
		[value] =>         
	)     
	[3] => Array (             
		[name] => set_id            
		[value] =>         
	)
	[4] => Array (             
		[name] => field_id            
		[value] =>         
	)
	[5] => Array (             
		[name] => planned_id            
		[value] =>         
	)
 --- The array continues looping through all the fields about giving me 200+ subarrays
	[2xx] => Array (           
		[name] => Weight
		[value] =>         
	)     
	[2xx] => Array (             
		[name] => repetition             
		[value] =>         
	)
	[2xx] => Array (           
		[name] => exercise_id
		[value] =>         
	)     
	[2xx] => Array (             
		[name] => set_id            
		[value] =>         
	)
	[2xx] => Array (             
		[name] => field_id            
		[value] =>         
	)
	[2xx] => Array (             
		[name] => planned_id            
		[value] =>         
	)

 

24 minutes ago, Stoffer said:

So far I can only transfer the data from jQuery in one of the 2 ways mentioned above

I managed to create multiple flat arrays, but struggling to map them into arrays and order them together based on exercise_id > set_id. In this case I would end up with 18 arrays, that contains data from each of the arrays below.

Array
(
    [field_id] => Array
        (
            [0] => h
            [1] => h
            [2] => h
            [3] => h
            [4] => h
            [5] => h
            [6] => h
            [7] => h
            [8] => h
            [9] => h
            [10] => h
            [11] => h
            [12] => h
            [13] => h
            [14] => h
            [15] => h
            [16] => h
            [17] => h
        )

    [planned_id] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 1
            [5] => 1
            [6] => 1
            [7] => 1
            [8] => 1
            [9] => 1
            [10] => 1
            [11] => 1
            [12] => 1
            [13] => 1
            [14] => 1
            [15] => 1
            [16] => 1
            [17] => 1
        )

    [exercise_id] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 1
            [3] => 1
            [4] => 13
            [5] => 13
            [6] => 13
            [7] => 13
            [8] => 13
            [9] => 14
            [10] => 14
            [11] => 14
            [12] => 15
            [13] => 15
            [14] => 15
            [15] => 16
            [16] => 16
            [17] => 16
        )

    [set_id] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 1
            [5] => 2
            [6] => 3
            [7] => 4
            [8] => 5
            [9] => 1
            [10] => 2
            [11] => 3
            [12] => 1
            [13] => 2
            [14] => 3
            [15] => 1
            [16] => 2
            [17] => 3
        )

    [weight] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
        )

    [reps] => Array
        (
            [0] => 
            [1] => 
            [2] => 
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
        )

)

 

One way is reorganise your form's field naming convention. Here is an example which will send the data in the format you want, IE

Array
(
    [1] => Array
        (
            [weight] => W1
            [repetition] => R1
            [field_id] => F1
            [exercise] => E1
            [planned_workout_id] => P1
        )

    [2] => Array
        (
            [weight] => W2
            [repetition] => R2
            [field_id] => F2
            [exercise] => E2
            [planned_workout_id] => P2
        )

    [3] => Array
        (
            [weight] => W3
            [repetition] => R3
            [field_id] => F3
            [exercise] => E3
            [planned_workout_id] => P3
        )

    [4] => Array
        (
            [weight] => W4
            [repetition] => R4
            [field_id] => F4
            [exercise] => E4
            [planned_workout_id] => P4
        )

    [5] => Array
        (
            [weight] => W5
            [repetition] => R5
            [field_id] => F5
            [exercise] => E5
            [planned_workout_id] => P5
        )

)

Example code

<?php

if ($_SERVER['REQUEST_METHOD']=='POST') {                                       // handle the AJAX request 
    if (isset($_POST['data'])) {
        exit(print_r($_POST['data'], 1));                                       // and return the data as the response
    }
}
 
?>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript">
        $().ready( function() {
            
            $("#btnSend").click( function() {
                $.post(
                    "",                                 // send ajax request to self
                    $("#form1").serialize(),
                    function(resp) {
                        $("#output").html(resp)
                    },
                    "TEXT"
                )
                
            })
             
        })
</script>        
</head>
<body>
    <form id="form1">
        <?php
            for ($i=1; $i<=5; $i++) {
                echo "Weight :     <input type='text' name='data[$i][weight]' value='W$i'><br>
                      Repetition : <input type='text' name='data[$i][repetition]' value='R$i'><br>
                      Field_id :   <input type='text' name='data[$i][field_id]' value='F$i'><br>
                      Exercise :   <input type='text' name='data[$i][exercise]' value='E$i'><br>
                      Planned workout : <input type='text' name='data[$i][planned_workout_id]' value='P$i'><hr>
                     ";
            }
        ?>
    </form>
    <button id="btnSend">Send</button>
    <br>
    <h3>Data received from form:</h3>
    <textarea cols="50" rows="50" id="output"></textarea>
</body>
</html>

 

Please consider using json for all transfer between your javascript and php.  It is the de facto standard way of doing this, and the main way anyone does REST/AJAX etc. these days.  With json_decode and json_encode, you can naturally and easily convert between json objects and arrays and php objects and/or arrays.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.