How to pass a value inside do() while; using form post?

lmsook

Guppy
Hello
Below is my code.


PHP:
<?php 
$borrowUserId = userId(); 

$sql = "SELECT transaction.id as id,  itemReturned, itemReturnedDate 
    FROM transaction 
    WHERE borrowUserId = '$borrowUserId' "; 
          
if ($shouldReturn = mysql_fetch_array($shouldReturns)) { 
    echo '<p>Hello, '. $username . '! Here are items you should return.</p>'; 
    echo '<form method="post" action="itemReturned.php">'; 
    echo '<table border="1" cellpadding="1" cellspacing="0">'; 
    $i = 1; 
    do { 
    $id = $shouldReturn['id']; 
      $returnByDate = $shouldReturn['returnByDate'];      $itemReturned = $shouldReturn['itemReturned']; 
       $itemReturnedDate = $shouldReturn['itemReturnedDate']; 
    
          
    echo '<tr><td><input type="hidden" name="transactionId" value="'.$id.'">'; 
if ($itemReturned) { 
    echo '<td><font color="#ff0000"> returned </font></td>'; 
    echo '<td>'.$itemReturnedDate.'</td></tr>'; 
} else { 
    echo '<td><font color="#C0C0C0"> not yet </font></td>'; 
                echo "<td><script>DateInput('itemReturnedDate', true, 'YYYY-MM-DD')</script>";    
          echo '<input type="submit" name = "itemReturned" value="Item Returned" /></td>'; 
    } 
    $i++;              
} while ($shouldReturn = mysql_fetch_array($shouldReturns)); 
    echo "</table></form>"; 
} 
?>

Each row has transactionId and submit button.
As you may guess, I would like to pass "transactionId" of the row to itemReturned.php when itemReturned submit button of that row is pressed.
So I used "hidden" input type, but as you see the hidden input type "transactionId" value always is the last one.
Of course in itemReturned.php file, I need to retrieve the transaction id value and insert into DB.
So when I use $_POST[transactionId] to get the hidden value in itemReturned.php,
it always shows the last transaction id of the do() while loop.
No wonder because it loops and assign the last id to the transactionId.
Now how can I pass individual id when individual submit button is pressed?
 
You need each hidden field and submit button pair to be their own form. Otherwise the value of last hidden field will be what gets passed to your form handler.

You're basically doing:
<form method="post" action="handler.php">
<input type="hidden" name="foo" value="1">
<input type="hidden" name="foo" value="2">
<input type="hidden" name="foo" value="3">
<input type="submit" value="Submit">
</form>

And on the handler, $_POST['foo'] will equal 3.

Alternatively, give each submit button a name set equal to the appropriate transaction ID. Then, on your form handler page, check for the existence of a form element by that name. (isset() for the other submit buttons that didn't get clicked on will return false.) Of course, this latter technique requires you to know ahead of time the value of all the IDs you'll be passing.

Tim
 
Back
Top