1

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?

2
  • Just as an addendum to all the answers, it is the "ID" field that you cannot have two of the same, that will cause problems in an HTML form. Commented Mar 21, 2014 at 12:38
  • Do you really mean "in PHP" or "in HTML".... it's possible to create any output in PHP, whether valid or not Commented Mar 21, 2014 at 12:41

4 Answers 4

5

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
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

1

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

Comments

1

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.

1 Comment

Hello! I noticed that you end your all answers with a “Thanks”, unfortunatly this is discouraged here on SO to leave place for the actual contents of your answer. Have a look at this Meta Post: meta.stackexchange.com/q/2950/174540 Also: Check your formatting before you submit your answer. You accidentally put your text inside the code block as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.