Multi-Dimensional Array Post from Form

Submitting a multidimensional array via POST with php

On submitting, you would get an array as if created like this:

$_POST['topdiameter'] = array( 'first value', 'second value' );
$_POST['bottomdiameter'] = array( 'first value', 'second value' );

However, I would suggest changing your form names to this format instead:

name="diameters[0][top]"
name="diameters[0][bottom]"
name="diameters[1][top]"
name="diameters[1][bottom]"
...

Using that format, it's much easier to loop through the values.

if ( isset( $_POST['diameters'] ) )
{
echo '<table>';
foreach ( $_POST['diameters'] as $diam )
{
// here you have access to $diam['top'] and $diam['bottom']
echo '<tr>';
echo ' <td>', $diam['top'], '</td>';
echo ' <td>', $diam['bottom'], '</td>';
echo '</tr>';
}
echo '</table>';
}

Sending multi-dimensional array by POST

Since you don't have any special characters in date, it is better to give without a ':

 echo "<input type='hidden' name=\"fixtures[$i]['team-a-name']\" value='$team_a_name'>";
echo "<input type='hidden' name=\"fixtures[$i]['team-b-name']\" value='$team_b_name'>";
echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";

Or change it to:

 echo "<input type='hidden' name=\"fixtures[$i][team-a-name]\" value='$team_a_name'>";
echo "<input type='hidden' name=\"fixtures[$i][team-b-name]\" value='$team_b_name'>";
echo "<input type='hidden' name=\"fixtures[$i][date]\" value='$fixtDate'>";

Always use var_dump($_POST) or print_r($_POST) to check what's happening in reality. :)

multi-dimensional array post from form

You can name your form elements like this:

<input name="users[1][name]" />
<input name="users[1][email]" />
<input name="users[2][name]" />
<input name="users[2][email]" />
...

You get the idea...

passing multidimensional array from form in php

You can specify the array key and also make it two dimensional:

name="site[1][]"
name="site[1][]"
name="site[2][]"
name="site[2][]"

Then loop through and use key and value:

foreach($_POST['site'] as $site => $addresses) { // $site is the number and $addresses is an array
$address_list = implode(',', $adresses); // or loop $addresses or whatever
}

There are a lot of possibilities depending on how is easiest to structure and access it in your particular case.

How 2D array is handled in HTTP POST request

By default, the form will be submitted with application/x-www-form-urlencoded content type, so your data will be URI-encoded by these rules.

Encoded:

myList%5B%5D=John&myList%5B%5D=Peter&myList%5B%5D=Mike&myList%5B%5D=Neo&myList%5B%5D=Stella&myList%5B%5D=Eve

Decoded:

myList[]=John&myList[]=Peter&myList[]=Mike&myList[]=Neo&myList[]=Stella&myList[]=Eve

If you want to submit only one parameter, you should encode your data on client side, e.g. you can separate all values by comma.

Here's a working example (using jQuery):

$('form').submit(function(e) {
processFormSubmission(this);
});

function processFormSubmission(formElem) {
var arr = [];
$(formElem).find(':input').each(function(i) {
$(this).attr('disabled', 'disabled'); // prevent from submitting
if( $(this).is(':submit') ) return true; // skip submit input field
arr.push(this.value);
});
$('<input>').attr({type: 'hidden', name: '_data', value: arr.join(',')}).appendTo(formElem);
}

The above code will create hidden _data input and will add disabled attribute to other input fields, so they will be not submitted to server.

_data=John,Peter,Mike,NeoStella,Eve

On server side you should decode that data, e.g. something like this:

// var_dump( $_POST );

if( ! empty( $_POST['_data'] ) {
$myList = explode(',', $_POST['_data']);
// var_dump( $myList );
}

PHP form submit into multi-dimensional array

You want to send an array of dictionaries to the server.

First, create an array that will contain dictionaries say var arrays = [] as your global variable.

var arrays = [];

The general idea is every time when a user has finish inputting data, you need to add that data to arrays.

Add a button to let jQuery know when to add to the arrays:

<button id="abutton" type="button" >Click Me!</button>

Here is how you add the data to the arrays in jQuery:

 $("#abutton").click(function(){

var nameFieldData = // get data from name field
var pageFieldData = // get data from page field

arrays.push(
{'nameField': nameFieldData,
'PageField': pageFieldData});
});

Then send arrays back to the server when you are ready.

How to create multidimensional array from php post?

You have made some simple errors

You can foreach over an array then you dont care how large or small it is. You also have a error in the variable used in $_POST.

Using the foreach( $arr as $index => $value) syntax gets you an index into the array you are processing, If all 3 arrays are always the same size you can then use that to reference the other arrays.

$arr = [];
foreach ( $_POST['location'] as $i => $locn ) {
$arr[$locn][] = [ $_POST['msg'][$i] => $_POST['order_id'][$i] ];
}

It help to debug your code if you

Add error reporting to the
top of your file(s) while testing right after your opening PHP tag for example.
Even if you are developing on a server configured as LIVE you will now see any errors.
<?php error_reporting(E_ALL); ini_set('display_errors', 1);



Related Topics



Leave a reply



Submit