Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve multiple check box values 2

Status
Not open for further replies.

Microbe

Programmer
Oct 16, 2000
607
AU
Frustrated.

I have a form
Code:
<?php
if (isset($_POST["Submit"]) && ($_POST["Submit"] > "")){

echo $_POST['textfield'];

}
?>
<form name="form1" method="post" action="">
  <p>
    <input type="text" name="textfield" value='1'>
    <input type="text" name="textfield" value='2'>
</p>
  <p>
    <input type="submit" name="Submit" value="Submit">
</p>
</form>
it only shows the value of the last textbox. Fair enough.

My understanding is that to get both values I need to use $HTTP_POST_VARIABLES, but when I do, it only has one value in teh array.

My background is with ASP and I am moving to PHP. With ASP I would have had a string 1,2 returned, but I can't even get forach() to work with this.

What am I doing wrong?

Thanks in advance

Steve Davis
ttf(a)HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
If you have inputs with the same name it will be only the value of the last one that is carried to the next page. At least that is the functionality I have been seeing. Just use different names and on the next page go through $_POST array with foreach if you want to grab all the values.
 
Thanks, posting values must work differently with PHP.

That worked.

Steve Davis
ttf(a)HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
You can easily get multiple values with one name. The trick is to use the pseudo-array naming convention.

If you have the HTML:

Code:
<html><body>
<form method="post action="somescript.php">
   <input type="text" name="foo[b][][/b]"><br>
   <input type="text" name="foo[b][][/b]"><br>
   <input type="text" name="foo[b][][/b]"><br>
   <input type="submit">
</form></body></html>

Then when the form is submitted to somescript.php, $_POST['foo'] will itself be an array.

This naming convention is also necessary when using a <SELECT> that allows multiple selections.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Ahh, thanks for that. I did manage to get it working, but this was the actual solution I was looking for.

And I toally agree with your tag re "want best answers". Whenever I ask a question I try to answer a bunch at the same time as a "payback".

It amazes me how badly some people frame their questions, even allowing for non English speakers.

The most common problem that (for me) makes a question unanswerable, is a HUGE slab of uncommented code.

That's why I cut my code down to a brief example in the original question.

Steve Davis
ttf(a)HaHaHa.com.au

Me? I can't even spell ASP!

NOTE: This sig does not include any reference to voting, stars, or marking posts as helpful as doing so is cause for membership termination.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top