Post an Array from an HTML Form Without JavaScript

POST an array from an HTML form without javascript

check this one out.

<input type="text" name="firstname">
<input type="text" name="lastname">
<input type="text" name="email">
<input type="text" name="address">

<input type="text" name="tree[tree1][fruit]">
<input type="text" name="tree[tree1][height]">

<input type="text" name="tree[tree2][fruit]">
<input type="text" name="tree[tree2][height]">

<input type="text" name="tree[tree3][fruit]">
<input type="text" name="tree[tree3][height]">

it should end up like this in the $_POST[] array (PHP format for easy visualization)

$_POST[] = array(
'firstname'=>'value',
'lastname'=>'value',
'email'=>'value',
'address'=>'value',
'tree' => array(
'tree1'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree2'=>array(
'fruit'=>'value',
'height'=>'value'
),
'tree3'=>array(
'fruit'=>'value',
'height'=>'value'
)
)
)

HTML Form: POST an array of objects

tl;dr: Add empty brackets ([]) after students to the input names.

Fiddling with Rack::Utils.parse_nested_query it seems you can get the payload you want like this:

<!-- first student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">

<!-- second student -->
<input type="text" name="students[][first]">
<input type="text" name="students[][last]">
<input type="text" name="students[][age]">

Note the empty brackets ([]) after students. This tells Rack you want the students param to be an array. Subsequent params encountered (with the same name) will start a new element.

POST /myroute?students[][first]=foo&students[][last]=bar&students[][age]=21&students[][first]=baz&students[][last]=qux&students[][age]=19

Gets parsed like this:

{"students" => [
{
"first" => "foo",
"last" => "bar",
"age" => "21"
},
{
"first" => "baz",
"last" => "qux",
"age" => "19"
}
]}

Further reading: http://codefol.io/posts/How-Does-Rack-Parse-Query-Params-With-parse-nested-query

Html Form With Array

An array is a list of data, it has no built-in visual representation (largely because it has no guaranteed structure within it). You need to decide how you want to display that data and write some code to loop through the array and output the data in the format you want.

At the absolute simplest you can just output each item on a separate line, or separated by commas. For that you need to build up the mail body string gradually so you can concatenate the values together. To make for less repetition, a function would be useful here, so you can use it to display the contents of all the arrays in a consistent way.

Something like this should work:

function arrayToString($arr)
{
$output = "";
foreach ($arr as $item) $output .= $item."<br>";
return $output;
}

$mail->Body ="

<p><h3> | ORÇAMENTO - ONLINE PEDIDO</h3></p>


<p><h3>⇰ Informação Cliente:</h3></p>


<h3>▪ Tipo de cliente ( 0 = Particular | 1 = Empresa):</h3> $empresa <br>
<h3>▪ Nome Empresa:</h3> $empresa_nome <br>
<h3>▪ Montagem ( 0 = Nao Quero Montagem | 1 = Quero Montagem):</h3> $montagem <br>
<h3>▪ Local de montagem:</h3> $morada_montagem <br>
<h3>▪ Primeiro Nome:</h3> $name <br>
<h3>▪ Ultimo Nome: </h3>$nomeultimo <br>
<h3>▪ Email:</h3> $email <br>
<h3>▪ Nº Telefone:</h3> $phone <br>
<h3>▪ NIF:</h3> $nif <br>
<h3>▪ Morada: </h3>$morada <br>
<h3>▪ Cidade: </h3>$localidade <br>
<h3>▪ Código Postal:</h3> $codigopostal <br>

<p><h3>⇰ Informação Produto:</h3></p>
---------------------------------------------<br>




<p><h3>⇰ Produtos:</h3></p>
<h3>▪ Tipo de Estore:</h3>".arrayToString($Caracteristicas)."
<h3>▪ Largura:</h3> ".arrayToString($Largura)."
<h3>▪ Altura: </h3>".arrayToString($Altura)."
<h3>▪ Quantidade:</h3>".arrayToString($Quantidade)."<br>
---------------------------------------------<br>


<h3>▪ Acionamento:</h3> $adicionamento <br>

<h3>▪ Visita do Tecnico:</h3> $tecnico <br>

<h3>▪ Mensagem :</h3> $message<br>
<p><h3>Cliente aceita (política de privacidade)</h3>$politicaprivacidade<br></p>

<p><img src=\"cid:logoimg\" /></p>";

How to send array using html form

This is not specific to Symfony2. Its basic HTML. You have to supply multiple input with appropriate name. You cannot sent an array value in a single input element!! Thats basic HTML!!

<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty[0][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[1][iloscuser]" value = "specific-value-from-sniadanie">
<input type="hidden" name = "produkty[2][iloscuser]" value = "specific-value-from-sniadanie">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>

But you can send a json string inside a single element.

<form action="{{ path('_przepisy') }}" method="post">
<input type="hidden" name = "produkty" value = "{{ sniadanie | serialize }}">
<input type="submit" class="btn btn-success pull-right" value="Przepisy"/>
</form>

Then in controller

$this->render("your view", [
'produkty' => json_decode($request->get('produkty'))
]);

And in the template

{{produkty[0]['iloscuser']}}

Send input array with jquery Post without submitting form

You can get all the values in all inputs named "stuff[]" like this:

$('#stuff[]').map(function() { return $(this).val(); }).get().join()

This will give you a string concatenating all values, separated by commas.

Use that inside your code like this:

function send() {
params= {'name': $('#name').val(),
'stuff': $('input[name="stuff[]"]').map(function() {
return $(this).val(); }).get().join()};
$.post( "url/test", params );
}

Send array of objects as POST data without ajax

You need to stringify the object you concatenate to the HTML:

form.append('<input type="hidden" name="filedata[]" value="' + JSON.stringify(filedata) + '"/>');

You then need to deserialize that value in your PHP:

foreach($_POST['filedata'] as $d => $data)
{
$obj = json_decode($data);
echo '<pre>';
print_r($obj['name']);
print_r($obj['ordernum']);
print_r($obj['size']);
echo '</pre>';
}

How to Submit empty array from HTML Form Post to PHP

No, you can't (although it seems pointless to know it).

Once you set an array-type form field, it yields as much indexes as there are values to this. In that case, it's an empty string, meaning its value was nothing (''), just because it exists. To PHP, having no explicit value means being something null or empty, which counts as valid value. Nothing means not being declared or defined at all, which is not the case of an empty array, or an array of empty value(s).