Frage

In php, i have some input boxes in a form tag

Is it possible to create two input box with same name? Is there any problem while the form is submitted?

War es hilfreich?

Lösung

Is it possible to create two input box with same name?

Yes. Element ids must be unique, but names do not.

Is there any problem while the form is submitted?

PHP will discard all but the last one unless the name ends in [].

<input name="foo[]">
<input name="foo[]">

will be accessible via

$_POST['foo'] # an array

Andere Tipps

there is always the possibility to use array like syntax e.g.

<input type="text" name="my-field[]">
<input type="text" name="my-field[]">

in php you can access it using something like

$_REQUEST['my-field'][0]

You may add the array bracket [] onto each input box. like this;

<form method="post" action="somepage.php">
<input type="text" name="box[]">
<input type="text" name="box[]">
<input type="submit" name="submit" value="Submit">
</form>

Then on somepage.php

<?php
  if(isset($_POST['submit']))
  {
     echo $_POST['box'][0];
     echo $_POST['box'][1];
  }
?>

The [] makes two boxes to be submitted as an array

Yes you can create with array parameter.

<form>
    <input type="text" name="txtname[]">
    <input type="text" name="txtname[]">
</form>

You will get value using $_REQUEST['txtname'] which is array.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top