Istvan Nagy
Istvan Nagy

Reputation: 113

How to get values[] in php?

I have this code

HTML

<ul style="list-style:none;" id="listing">
<li>
<table id="pedido" action="post.php">
<form method="POST" >
<tr>
<td>
<select name="product[]">
    <option value="0">Perfil:</option>
    <option value="68mm 5 Cámaras AD rendszer egyenes szárny, ütköző tömítéssel">68mm 5 Cámaras 7001AD, con dos juntas</option>
    <option value="68mm 7 K AD rendszer íves szárny, ütköző tömítéssel">68mm 7 Cámaras 7001AD con dos juntas</option>
    <option value="80 mm 6 K Tok + 7 K íves szárny  AD rendszer, ütköző tömítéssel">80 mm Marco 6 Cámaras  + Hoja de 7 Cámaras 7001AD con dos juntas</option>
    <option value="68mm 7 K  MD rendszer ütköző és középtömítéses, íves szárny">68mm 7 Cámaras 7001MD con tres juntas</option>
    <option value="80 mm 8 K MD rendszer ütköző és középtömítéses, íves szárny">80 mm 8 Cámaras 7001MD con tres juntas</option>
</select>
</td>
</tr>
<tr>
<td>
<input name="h[]" value="Horizontal" class="comment" /> x
<input name="v[]" value="Vertical" class="comment" />
uds:
<input name="uds[]" style="width:60px;"/>
</td>
</tr>
<tr>
<td>
<select>
<option>Color</option>
</select>

Persiana <input type="checkbox" name="persiana[]" id="persiana"/> <div id="persianaver" style="float:right; display:none">Con motor<input type="radio" name="f[]" value="auto" /> Manual<input type="radio" name="f[]" value="manual" /></div>
</td>
<tr></tr>
<tr>
<td style="width:435px;">
<div id="persiaops" style="float:right; display:none">IMPORTANTE: En caso de haber seleccionado la opción persiana usted tiene que especificar el tamaño de la tapa del cajón (Lugar donde se sitúa la caja de persiana) Puede especificarlo en el campo comentario <a id="pregunta" href="images/demo.jpg"><img alt="pregunta" src="images/ask.jpg"  height="15" width="15"/></a> </div>
</td>
</tr>
<tr>
<td>
<textarea name="comment[]" rows="5" cols="59" class="comment">Comentario</textarea>
</td>
</tr>
</form>
</table>
</li>
</ul>

And every element is called name[] because i want to clone the table many times and then fill the information. But my question is: How can i get those vales in php? I mean by $_POST['hello'] I can get the hello field's content if its submitted but what about $_POST['hello[]']?? It will be stored as an array object.. but how can I check if all the fields with name="hello" are filled correctly? Or I will need to store the values as an array and later in the post.php check if all the elements contained by the array are correct? Is there an easy way?

Thanks ;)

Upvotes: 0

Views: 130

Answers (3)

lszrh
lszrh

Reputation: 1582

Fields named like product[] can be accessed in PHP with $_POST["product"][0] (for the first one).

You can also loop through this array:

foreach($_POST["product"] as $key => $product) {
    echo $product;
}

To check whether all your fields are filled, you should add a run-time variable, which is counted up every time you clone your input fields:

<select name="product[0]">...
...
<select name="product[1]">...
...

Then you can loop again through the products:

foreach($_POST["product"] as $key => $product) {
    //check whether h is also filled:
    if($_POST["h"][$key] != "") {
        ...
    }
}

Upvotes: 2

DaveRandom
DaveRandom

Reputation: 88697

They will be interpreted as arrays when PHP parses the request data. So:

<input name="v[]" value="Vertical" class="comment" />
<input name="v[]" value="Horizontal" class="comment" />

Will be available in PHP as $_POST['v'] and will look like:

Array
(
    [0] => Vertical
    [1] => Horizontal
)

It is the same basic syntax as a PHP implicit array push:

$v[] = 'Vertical';
$v[] = 'Horizontal';

Upvotes: 1

antyrat
antyrat

Reputation: 27765

You can get them as arrays:

echo $_POST['product'][0];

To see your $_POST arrays structure just write:

print_r($_POST);

Upvotes: 0

Related Questions