3

I'm hoping to solve a problem with a baseball team! ;)

I've a form with multiple textboxes - each of which has a series of checkboxes associated with it. To get a better feel, create a file with the following, and open in a browser. A picture speaks a thousand words.

<html>
<head></head>
<body>
        <h2>Enter Player Name and Positions They Can Play:</h2>
                        <form name="player" method="POST">

                <p>Player 01: <input type="text" name="p1" value="" />
                | P <input type="checkbox" name="pitch1" value="P" />
                | C <input type="checkbox" name="catch1" value="C" />
                | 1B <input type="checkbox" name="first1" value="1B" />
                | 2B <input type="checkbox" name="second1" value="2B" />
                | SS <input type="checkbox" name="short1" value="SS" />
                | 3B <input type="checkbox" name="third1" value="3B" /> |</p>

                <p>Player 02: <input type="text" name="p2" value="" />
                | P <input type="checkbox" name="pitch2" value="P" />
                | C <input type="checkbox" name="catch2" value="C" />
                | 1B <input type="checkbox" name="first2" value="1B" />
                | 2B <input type="checkbox" name="second2" value="2B" />
                | SS <input type="checkbox" name="short2" value="SS" />
                | 3B <input type="checkbox" name="third2" value="3B" /> |</p>

                <p>Player 03: <input type="text" name="p3" value="" />
                | P <input type="checkbox" name="pitch3" value="P" />
                | C <input type="checkbox" name="catch3" value="C" />
                | 1B <input type="checkbox" name="first3" value="1B" />
                | 2B <input type="checkbox" name="second3" value="2B" />
                | SS <input type="checkbox" name="short3" value="SS" />
                | 3B <input type="checkbox" name="third3" value="3B" /> |</p>
    <p><input type="submit" value="Submit" name="Submit" /></p>
    </form>
</body>
</html>

So, if a user typed in:

"Fred" and checked 1B, SS, 3B (first base and shortstop)

"Ted" and checked P, 2B (pitcher and second base)

"Joe" and checked P, 3B (pitcher and third base)

On submit, I'd like to see the following array (named roster, with sub-arrays named from the textbox entry and the sub-array contents only the checked values:

roster(

Fred(1B,SS,3B),

Ted(P,2B),

Joe(P,3B)

)

I'm confident I can handle everything beyond this... I'm just having a devil of a time figuring out how to create each sub array and associating them with the textboxes and adding the checkbox values to each array.

Any help is much appreciated! Thank you!

2
  • 1
    nope, it's to dynamically create line-up cards for a youth league I coach. Commented Apr 28, 2013 at 23:18
  • 1
    Well that's better :-D. Good on you! Commented Apr 29, 2013 at 0:28

2 Answers 2

3

It's not exactly what you asked, but a better way to manage this kind of forms is to choose vars names like this:

<p>Player 01: <input type="text" name="player[1][name]" value="" />
     | P <input type="checkbox" name="player[1][pitch]" value="P" />
            | C <input type="checkbox" name="player[1][catch]" value="C" />
            | 1B <input type="checkbox" name="player[1][first]" value="1B" />
            | 2B <input type="checkbox" name="player[1][second]" value="2B" />
            | SS <input type="checkbox" name="player[1][short]" value="SS" />
            | 3B <input type="checkbox" name="player[1][third]" value="3B" /> |</p>

            <p>Player 02: <input type="text" name="player[2][name]" value="" />
            | P <input type="checkbox" name="player[2][pitch]" value="P" />
            | C <input type="checkbox" name="player[2][catch]" value="C" />
            ...

You will obtain something like that:

Array
(
    [player] => Array
    (
        [1] => Array
        (
            [name] => Marcel
            [pitch] => P
            [short] => SS
            [third] => 3B
        )
        [2] => Array
        (
            [name] => Auguste
            [catch] => C
            [first] => 1B
        )
        [3] => Array
        (
            [name] => Ulysse
            [catch] => C
            [second] => 2B
        )
    )
)

Then, you can easily access each element of each player:

$players = $_POST['player'];

echo $players[1]['name'] . '<br/>';

or you can construct this kind of array:

foreach($players as $key=>$player) {
    $temp[] = $player['name'];
    unset($player['name']);
    $temp[] = $player;
    $result[] = $temp;
    unset($temp);
}
print_r($result);
Sign up to request clarification or add additional context in comments.

Comments

0

You may try this but it's better to use what Casimir et Hippolyte recommended.

if( isset($_POST['Submit']) )
{
    $players = $_POST['players'];
    $items = array('pitch', 'catch', 'first', 'second', 'short', 'third');
    $roaster = array();
    $i=1;
    foreach($players as $player)
    {
        if(!$player) continue;
        $roaster[$player]=array();
        foreach($items as $item)
        {
            if( isset( $_POST[$item.$i] ) ) $roaster[$player][] = $_POST[$item.$i];
        }
        $i++;
    }
    $roaster = array_filter( $roaster );
}

Just make some changes in your form as follows

Player 01: <input type="text" name="players[]" value="" />
...
Player 02: <input type="text" name="players[]" value="" />
...
Player 03: <input type="text" name="players[]" value="" />
...

4 Comments

Sheikh - Would I change anything in the form with the checkboxes? It's that part that screws me up... Like: <input type="checkbox" name="item[]" value="P" />
No, Just change p1, p2, p3 = players[].
Your pitch1, pitch2 etc should remain the same as you posted in your question.
cool, gonna give it a whirl. Casimir's works nicely, but your processing of the form data was similar to what I've been trying - so I'm more inclined to see if this yields the results I was hoping for.

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.