2

Here is my HTML code:

    <form name="candidates" action="candidate_thankyou.php" method="post">
    <div class="wrap"> 
        <div class="position"> President: <br> </div>
            <input type="text" name="president"> <br>
            <input type="text" name="president"> <br>
            <input type="text" name="president"> <br>
    </div>
    <div class="wrap"> 
        <div class="position"> Vice President: <br> </div>
            <input type="text" name="vp"> <br>
            <input type="text" name="vp"> <br>
            <input type="text" name="vp"> <br>
    </div> ...etc
    <input id="submit" type="submit" value="Submit">
    </form>

However, when I call var_dump($_POST) in my php file, I only get the last text box value for each input name (eg "president" or "vp"). If I only use the first 2 text boxes, I get an empty string. How can I get all three values from the form?

3 Answers3

3

Form fields are identified by their name. You're overriding the fields president/vp every time, that's why you only get the last field's values. You can send it as an array like this:

<input type="text" name="president[]"> 
<input type="text" name="president[]"> 
..
Sven van Zoelen
  • 6,989
  • 5
  • 37
  • 48
0

You should have seperate names for each input or add [] to the end of the names to add them to an array. Ie

name="vp[]"
Gavin
  • 2,123
  • 1
  • 15
  • 19
0

Field name must be either unique (president1, president2, etc.) or an array president[].

Further handling depends on which method you choose. Second method is well described in HTML form input tag name element array with JavaScript

Community
  • 1
  • 1
Mike
  • 1,158
  • 5
  • 22
  • 32