$_Post Array from HTML Form

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

$_POST Array from html form

Change

$info=$_POST['id[]'];

to

$info=$_POST['id'];

by adding [] to the end of your form field names, PHP will automatically convert these variables into arrays.

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).

<form method="POST" id="test" action="test.php"> <input type='hidden' name='items[]' /></form><input type="submit" value="Send" form="test">

PHP $_POST arrays with multiple rows

There are many ways to do this.
One would be:

<?php $states = array(1,2,3); ?>
<form method="post" action="test.php">
<?php foreach ( $states as $state ) : ?>
<input type="hidden" name="<?php echo 'state['.$state.']'; ?>[IDNo]" />
<input type="hidden" name="<?php echo 'state['.$state.']'; ?>[Name]" />
<input type="hidden" name="<?php echo 'state['.$state.']'; ?>[Date]" />
<input type="hidden" name="<?php echo 'state['.$state.']'; ?>" />
<?php endforeach; ?>
<input type="submit" value="Test Me" />
</form>

You can access these values by using code like $_POST['state'][1]['Name']

You can expand the $states array to hold more information.

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 store POST values for each form in an array?

You don't need multiple forms or hidden inputs to achieve this. You can just use buttons, and set their values to $i.

echo "<form id='upload' action='test2.php' method='POST'>";
for ($i = 0; $i < 10; $i++) {
echo "<button type='submit' name='test' value='$i'>Go to album</button>";
}
echo '</form>';

In test2.php, $_POST['test'] will have the $i value of the button you clicked.

Posting array from form

Give each input a name in array format:

<input type="hidden" name="data[EstPriceInput]" value="" />

Then the in PHP $_POST['data']; will be an array:

  print_r($_POST);         // print out the whole post
print_r($_POST['data']); // print out only the data array


Related Topics



Leave a reply



Submit