Does my code is correct to handle two submit form in a loop? I have this code but it doesn't get the second form. I mean it redirects to selectedSold.php but it doesn't view any detail or it doesn't get the name="nganga"
of the selected row. In the first form it works then I decide to copy paste the code to selectedSold.php and then change a little bit of code in the select query but it doesn't show any result.
<?php foreach($articles as $a):?>
<tr>
<form method="post" action="selected.php">
<input type="hidden" name="peopleName" value=" <?php echo $a['name']; ?>">
<td><button class="btn btn-danger btn-sm btn-block" type="submit" name="viewing">Ordered</button></td>
</form>
<form method="post" action="selectedSold.php">
<input type="hidden" name="nganga" value=" <?php echo $a['name']; ?>">
<td><button class="btn btn-danger btn-sm btn-block" type="submit" name="psold">Bought</button></td>
</form>
<td><?php echo $a['name'];?></td>
<td><?php echo $a['contact'];?></td>
<td><?php echo $a['address'];?></td>
<td><?php echo $a['email'];?></td>
<td><?php echo $a['gender'];?></td>
<td><?php echo $a['datejoin'];?></td>
</tr>
<?php endforeach;?>
This is my query in first form ->
$articles = $db->prepare("
SELECT SQL_CALC_FOUND_ROWS * FROM ordered
WHERE buyername='".$_POST['peopleName']."'
LIMIT {$start}, {$perPage}
");
And this is my query in 2nd form
$articles = $db->prepare("
SELECT SQL_CALC_FOUND_ROWS FROM `sold`
WHERE buyername='".$_POST['nganga']."'
LIMIT {$start}, {$perPage}
");
Don't put spaces around <?php ... >?
.
<input type="hidden" name="peopleName" value="<?php echo $a['name']?>">
You can't submit 2 forms with one button, but you can have 2 (or more) forms in a loop submitting separately. So presuming that you only want to submit one form at a time, this should fix your issue. I have moved the <forms>
inside the <td>
s and changed the <buttons>
to <inputs>
.
<?php foreach($articles as $a):?>
<tr>
<td>
<form method="post" action="selected.php">
<input type="hidden" name="peopleName" value="<?=$a['name']?>">
<input type="submit" class="btn btn-danger btn-sm btn-block" name="viewing" value="Ordered"/>
</form>
</td>
<td>
<form method="post" action="selectedSold.php">
<input type="hidden" name="nganga" value="<?=$a['name']?>">
<input type="submit" class="btn btn-danger btn-sm btn-block" name="psold" value="Bought"/>
</form>
</td>
<td><?php echo $a['name'];?></td>
<td><?php echo $a['contact'];?></td>
<td><?php echo $a['address'];?></td>
<td><?php echo $a['email'];?></td>
<td><?php echo $a['gender'];?></td>
<td><?php echo $a['datejoin'];?></td>
</tr>
<?php endforeach;?>
Option 2 : for testing
This would achieve the same effect, and could be used for testing a simplified version.
<?php foreach($articles as $a):?>
<tr>
<td>
<a href="selected.php?peopleName=<?=$a['name']?>">Ordered</a>
</td>
<td>
<a href="selectedSold.php?nganga=<?=$a['name']?>">Bought</a>
</td>
<td><?php echo $a['name'];?></td>
<td><?php echo $a['contact'];?></td>
<td><?php echo $a['address'];?></td>
<td><?php echo $a['email'];?></td>
<td><?php echo $a['gender'];?></td>
<td><?php echo $a['datejoin'];?></td>
</tr>
<?php endforeach;?>