1

I have a while loop that gathers information from my DB. I then echo that out like this...

$num = 1;
$i=0;
$drop = 'yes';
echo '<form id="drop_form" method="post" action="here.php">';

while( $row = mysql_fetch_assoc($query)) {

   $player[] = $row['player'];

   echo '<tr class="rows"><td>'; echo'<input type="hidden"
   name="yeah" value="'.$num.'"/>
   <input name="submit" type="submit" value="submit"/>';

   echo $player[$i].'</td></tr>'; 

   $num++;                         
   $i++;
}
echo '</table>';
echo '</form>';

when I post my $num variable it always show up as the last possible number. So if there are 7 rows in that query then the number will be 7. I want to be able to click on the submit button and get the hidden value in the submit form.

Player 
mike    hidden number = 1
chris   hidden number = 2
jim     hidden number = 3
dan     hidden number = 4
8
  • 1
    Obligatory: The mysql_* functions will be deprecated in PHP 5.5. It is not recommended for writing new code as it will be removed in the future. Instead, either the MySQLi or PDO and be a better PHP Developer. Commented Jul 10, 2013 at 17:35
  • 3
    You're generating a form with several inputs, all called yeah, with different values. When you post the form, the earlier ones will be over-written by later ones, so the only one actually submitted is the last one. Commented Jul 10, 2013 at 17:35
  • 1
    Your { and } don't match up. Commented Jul 10, 2013 at 17:35
  • ok thanks andrewsi... should i change the name of the inputs for each one? Commented Jul 10, 2013 at 17:37
  • 2
    @Ricky: Either that, or use name="yeah[]". That will post all the values as an array ($_POST['yeah'] will be an array). Commented Jul 10, 2013 at 17:38

2 Answers 2

1

Your form is submitting something like yeah=1&yeah=2&yeah=3... This is equivalent to the following PHP:

$_POST['yeah'] = 1;
$_POST['yeah'] = 2;
$_POST['yeah'] = 3;

From this you can see that the variable is being overwritten.

Try using name="yeah[]", as this will result in an array, as follows:

$_POST['yeah'][] = 1;
$_POST['yeah'][] = 2;
$_POST['yeah'][] = 3;

Resulting in Array(1,2,3);

Sign up to request clarification or add additional context in comments.

Comments

1

Add this before the start of your while loop: $player = array();

You should always define arrays before a loop :)

Hope this helps! :)

Also:

1.Change name="yeah" to name=yeah[] as you want this input to be an array. 2.Move the submit button outside of the while loop as you should only need one of these.

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.