943,948 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 804
  • PHP RSS
Nov 25th, 2008
0

Get Variables values

Expand Post »
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???
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
zanzo is offline Offline
58 posts
since Feb 2008
Nov 25th, 2008
0

Re: Get Variables values

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:
html Syntax (Toggle Plain Text)
  1. <form method='post'>
  2. <input type='text' name='box1' size=30><br>
  3. <input type='text' name='box2' size=30>
  4. </form>
Then to retrieve those 2 fields and display them you would use the following php code:
php Syntax (Toggle Plain Text)
  1. echo $_POST['box1']; //displays first field in above example
  2. echo "<br>"; // adds new html line.
  3. 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
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Nov 25th, 2008
0

Re: Get Variables values

The textboxes should have similar names. In asp I get the values of both separated by commas, how to get that in php
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
zanzo is offline Offline
58 posts
since Feb 2008
Nov 25th, 2008
0

Re: Get Variables values

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.
php Syntax (Toggle Plain Text)
  1. <form method='post'>
  2. <input type='text' name='test[0]' value='111'><br>
  3. <input type='text' name='test[1]' value='222'><input type=submit value='submit'></form><br>
  4. <?
  5. var_dump($_POST); //dumps the variable
  6. echo "<p><hr>";
  7. echo $_POST['test'][0];
  8. echo "<br>";
  9. echo $_POST['test'][1];
  10. ?>
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.
Sponsor
Featured Poster
Reputation Points: 410
Solved Threads: 258
Occupation: Genius
cwarn23 is offline Offline
3,004 posts
since Sep 2007
Nov 25th, 2008
0

Re: Get Variables values

well I found the way to solve this issue:

php Syntax (Toggle Plain Text)
  1. <form method='post'>
  2.  
  3. <input name="test[]" type="text" />
  4. <input name="test[]" type="text" />
  5. <br>
  6. <input type=submit value=submit>
  7. </form>
  8.  
  9. <?
  10. echo $test[0]."<br>".$test[1];
  11. ?>
Last edited by peter_budo; Nov 27th, 2008 at 7:51 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
zanzo is offline Offline
58 posts
since Feb 2008
Nov 26th, 2008
0

Re: Get Variables values

Click to Expand / Collapse  Quote originally posted by zanzo ...
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.............
php Syntax (Toggle Plain Text)
  1. <table width="287" border="0" cellspacing="0" cellpadding="0">
  2.  
  3. <tr>
  4. <td colspan="5"><form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  5. <table width="285" border="0" cellspacing="0" cellpadding="0">
  6.  
  7. <tr>
  8. <td>&nbsp;</td>
  9. <td>name</td>
  10. <td><input type="text" name="cool" /></td>
  11. </tr>
  12. <tr>
  13. <td>&nbsp;</td>
  14. <td>&nbsp;</td>
  15. <td>&nbsp;</td>
  16. </tr>
  17. <tr>
  18. <td>&nbsp;</td>
  19. <td>address</td>
  20. <td><input type="text" name="cool" /></td>
  21. </tr>
  22. <tr>
  23. <td>&nbsp;</td>
  24. <td><input type="submit" name="Submit" value="Submit" /></td>
  25. <td>&nbsp;</td>
  26. </tr>
  27. </table>
  28. </form> </td>
  29. </tr>
  30.  
  31. <tr>
  32. <td width="129"><?php
  33. if(isset($_POST['Submit']))
  34. {
  35. echo "name".$_POST['cool'];
  36. echo "adderss".$_POST['cool'];
  37. }
  38. else
  39. {
  40. echo "yu didn't click in go button";
  41. }
  42. ?></td>
  43. <td width="76">&nbsp;</td>
  44. <td width="76">&nbsp;</td>
  45. <td width="76">&nbsp;</td>
  46. <td width="5">&nbsp;</td>
  47. </tr>
  48. </table>
Last edited by peter_budo; Nov 27th, 2008 at 7:53 pm. Reason: Please use [code] not <code>
Reputation Points: 15
Solved Threads: 5
Junior Poster
rohitrohitrohit is offline Offline
120 posts
since Oct 2007
Nov 26th, 2008
0

Re: Get Variables values

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

php Syntax (Toggle Plain Text)
  1. <table width="287" border="0" cellspacing="0" cellpadding="0">
  2.  
  3. <tr>
  4. <td colspan="5"><form id="form" name="form" method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
  5. <table width="285" border="0" cellspacing="0" cellpadding="0">
  6.  
  7. <tr>
  8. <td>&nbsp;</td>
  9. <td>name</td>
  10. <td><input type="text" name="cool[]" /></td>
  11. </tr>
  12. <tr>
  13. <td>&nbsp;</td>
  14. <td>&nbsp;</td>
  15. <td>&nbsp;</td>
  16. </tr>
  17. <tr>
  18. <td>&nbsp;</td>
  19. <td>address</td>
  20. <td><input type="text" name="cool[]" /></td>
  21. </tr>
  22. <tr>
  23. <td>&nbsp;</td>
  24. <td><input type="submit" name="Submit" value="Submit" /></td>
  25. <td>&nbsp;</td>
  26. </tr>
  27. </table>
  28. </form> </td>
  29. </tr>
  30.  
  31. <tr>
  32. <td width="129"><?php
  33. if(isset($_POST['Submit']))
  34. {
  35. echo "name".$_POST['cool'][0];
  36. echo "adderss".$_POST['cool'][1];
  37. }
  38. else
  39. {
  40. echo "yu didn't click in go button";
  41. }
  42. ?></td>
  43. <td width="76">&nbsp;</td>
  44. <td width="76">&nbsp;</td>
  45. <td width="76">&nbsp;</td>
  46. <td width="5">&nbsp;</td>
  47. </tr>
  48. </table>
Last edited by peter_budo; Nov 27th, 2008 at 7:54 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
zanzo is offline Offline
58 posts
since Feb 2008
Nov 26th, 2008
0

Re: Get Variables values

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];
?>
Reputation Points: 3
Solved Threads: 15
Posting Whiz
Aamit is offline Offline
341 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Is Online chatting done with php?
Next Thread in PHP Forum Timeline: Square thumbnail php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC