I have two input textbox with the same name, when submiting the page I get only the value of the second textbox.

how can i get the value of both fields???

Recommended Answers

All 7 Replies

When you setup the textbox fields, make sure they both have names and that their names are unique. So something like below is an example:

<form method='post'>
<input type='text' name='box1' size=30><br>
<input type='text' name='box2' size=30>
</form>

Then to retrieve those 2 fields and display them you would use the following php code:

echo $_POST['box1']; //displays first field in above example
echo "<br>"; // adds new html line.
echo $_POST['box2']; //displays second field in above example

So try to make sure the field names are unique and in the form element it has method=post

The textboxes should have similar names. In asp I get the values of both separated by commas, how to get that in php

I have just done a few tests and I don't see how it is possible to have the two name= fields exactly identicle but what you can do is put arrays in the name= fields and retrieve the $_POST as a 2 dimensional array. Below is an example of what I have done.

<form method='post'>
<input type='text' name='test[0]' value='111'><br>
<input type='text' name='test[1]' value='222'><input type=submit value='submit'></form><br>
<?
var_dump($_POST); //dumps the variable
echo "<p><hr>";
echo $_POST['test'][0];
echo "<br>";
echo $_POST['test'][1];
?>

Also you need to click the submit button for the above example to work. The above example shows about displaying what information the $_POST array holds and shows about using 2 dimensional $_POST arrays. Other than using arrays in the name= field,or changing the name of the field to be unique, you will find that only the last field of its duplicate name will be recorded in php.

well I found the way to solve this issue:

<form method='post'>

	<input name="test[]" type="text" />
	<input name="test[]" type="text" />  
<br>
	<input type=submit value=submit>
</form>

<?   
    echo $test[0]."<br>".$test[1]; 
?>

I have two input textbox with the same name, when submiting the page I get only the value of the second textbox.

how can i get the value of both fields???

i think same name textbox have to display there value.i tried some code [php].they are executing well.if u have still proble... then use id for controls.below code working good.............

<table width="287" border="0" cellspacing="0" cellpadding="0">

  <tr>
    <td colspan="5"><form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
      <table width="285" border="0" cellspacing="0" cellpadding="0">

        <tr>
          <td>&nbsp;</td>
          <td>name</td>
          <td><input type="text" name="cool" /></td>
          </tr>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          </tr>
        <tr>
          <td>&nbsp;</td>
          <td>address</td>
          <td><input type="text" name="cool" /></td>
          </tr>
        <tr>
          <td>&nbsp;</td>
          <td><input type="submit" name="Submit" value="Submit" /></td>
          <td>&nbsp;</td>
          </tr>
      </table>
        </form>    </td>
  </tr>

  <tr>
    <td width="129"><?php 
    if(isset($_POST['Submit']))
    {
         echo "name".$_POST['cool'];
         echo "adderss".$_POST['cool'];
    }
    else
    {
       echo "yu didn't click in go button";
    }
     ?></td>
    <td width="76">&nbsp;</td>
    <td width="76">&nbsp;</td>
    <td width="76">&nbsp;</td>
    <td width="5">&nbsp;</td>
  </tr>
</table>

Actually it doesn't work !!! But I fixed it and it works like that:

<table width="287" border="0" cellspacing="0" cellpadding="0">

<tr>
<td colspan="5"><form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="285" border="0" cellspacing="0" cellpadding="0">

<tr>
<td>&nbsp;</td>
<td>name</td>
<td><input type="text" name="cool[]" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>address</td>
<td><input type="text" name="cool[]" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit" /></td>
<td>&nbsp;</td>
</tr>
</table>
</form> </td>
</tr>

<tr>
<td width="129"><?php 
if(isset($_POST['Submit']))
{
echo "name".$_POST['cool'][0];
echo "adderss".$_POST['cool'][1];
}
else
{
echo "yu didn't click in go button";
}
?></td>
<td width="76">&nbsp;</td>
<td width="76">&nbsp;</td>
<td width="76">&nbsp;</td>
<td width="5">&nbsp;</td>
</tr>
</table>

I think it solve your problem

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">

</head>

<body>
<table width="287" border="0" cellspacing="0" cellpadding="0">

<tr>
<td colspan="5"><form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<table width="285" border="0" cellspacing="0" cellpadding="0">

<tr>
<td>&nbsp;</td>
<td>name</td>
<td><input type="text" name="textfield[]"  id="textfield[0]"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>address</td>
<td><input type="text" name="textfield[]"  id="textfield[1]" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" value="Submit"  onClick=" return form()"/></td>
<td>&nbsp;</td>
</tr>
</table>
</form> </td>
</tr>

<tr>
<td width="129">


</td>
<td width="76">&nbsp;</td>
<td width="76">&nbsp;</td>
<td width="76">&nbsp;</td>
<td width="5">&nbsp;</td>
</tr>
</table>
</body>
</html>
<? 
echo "name =".$_POST['textfield'][0];
echo"<br>";
echo "address =".$_POST['textfield'][1];
?>
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.