문제

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?

도움이 되었습니까?

해결책

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

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top