Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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
share|improve this question
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. –  Jason McCreary Jul 10 '13 at 17:35
    
thanks ill be on it –  Ricky Jul 10 '13 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. –  andrewsi Jul 10 '13 at 17:35
1  
Your { and } don't match up. –  Rocket Hazmat Jul 10 '13 at 17:35
2  
@Ricky: Either that, or use name="yeah[]". That will post all the values as an array ($_POST['yeah'] will be an array). –  Rocket Hazmat Jul 10 '13 at 17:38

2 Answers 2

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);

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.