1

I have searched for a solution to this problem, the closest I came was this answer, however it does not provide sufficient explanation.

The following button contains BOTH a tournaments name and a tournaments number.

<input type="submit" name="<?php echo $tournament.$weekNumber ?>" />

Tournaments can VARY i.e. have different names & round numbers. When the button is clicked and the form is submitted how do you get the tournament name and round number contained in the button name?

I have tried

foreach($_POST as $name => $content) { //echo "The TOURNAMENT NAME IS: $name <br>"; $tournament = $name; }

The problem...it gives me the round number AND tournament name concatenated....

I could use substr() to separate the tournament and round but it wont work since tournament names and round numbers can vary...

I need BOTH the tournament name and Week Number which is concatenated in the buttons name name="<?php echo $tournament.$weekNumber ?>"

2
  • try name="<?php echo $tournament."*".$weekNumber ?>". then explode using *. Commented Jan 19, 2017 at 9:50
  • You can use [] to submit an array. This might be a lot easier to handle than splitting a string. E.g. <button type="submit" name="t1[17]" value="foo">Submit</button> would result in something like $_POST['t1'][17] == 'foo'. Commented Jan 19, 2017 at 9:58

4 Answers 4

2

Try this: Separate the name with asymbol and then split.

<input type="submit" name="<?php echo $tournament.'_'.$weekNumber ?>"  />

foreach($_POST as $name => $content) {
        //echo "The TOURNAMENT NAME IS: $name <br>";
        list($tournament, $round) = explode('_', $name);
    }
Sign up to request clarification or add additional context in comments.

2 Comments

THIS WORKS GREAT. The only problem the round remains concatenated on the tournament name, but round displays perfectly. Example $tournament = PremeirLeague_5 $round = 5 How would I get rid of that _5 concatenated on the tournament name...? thanks
My bad figured it out simply use explode() function
2

When you concatenate both variables, you can add an unique string that you can use for splitting.

For example

<input type="submit" name="<?php echo $tournament.'|-|'.$weekNumber ?>"  />

In the PHP code, you can do this:

foreach($_POST as $name => $content) {
    //echo "The TOURNAMENT NAME IS: $name <br>";
    list ($tournament, $weekNumber) = explode('|-|', $name);
}

1 Comment

Thank you very much, will implement now and provide feedback
1

Why not do this:

<input type="submit" name="tournament[<?php echo $tournament ?>][<?php $weekNumber ?>]"  />

and then

if (isset($_POST["tournament"]) {
foreach ($_POST["tournament"] as $tournament => $weeks) {
       foreach ($weeks as $round=> $value) {
             //Do stuff
       }
}

Comments

1

why you don't use hidden value.

<input type="hidden" name="tournament" value="<?php echo $tournament ?>" />
<input type="hidden" name="weekNumber " value="<?php echo $weekNumber ?>" />

<input type="submit" name="<?php echo $tournament.$weekNumber ?>"  />

now you can get both value.

Comments

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.