Hi all

Here is a PHP question I have to answer.

Write a php file called reload.php that contains a table with 2 rows and 3 columns. In each of the cells of the top row of this table there should be the strings AAA, BBB & CCC. In the second row of this table there should be 3 submit buttons labeled ONE, TWO & THREE.

When the user presses one of these buttons, the same reload.php page should load, such that the string above the pressed button moves to the top left cell and the other two cells are filled with strings one further in the sequence. E.g. if the user presses the button below the string BBB, then the new page should have cells filled with the values BBB, CCC, DDD. If, in this page, the user now presses the button below the string DDD, the reloaded page should display the strings DDD, EEE, FFF. If the sequence ever gets beyond ZZZ, then it should wrap back to AAA.

Now the thing I'm having a problem with is...
When the form is submitted and the page reloads, how do I determine which button was pressed/which string was "chosen"? How would I structure that bit? I simply cannot of something that would work.

Would appreciate any help/ideas on this. Thanks.

In the submit button tag, you can define a "name" to it. For example,
<input name="AAA" value="Choose This" type="submit">

To capture which button was clicked. Use
if(isset($_POST))
{
// your script
}

If this doesn't work, then you may use three separate form for the table.

Excellent. Thank you so much, zippee. I had tried something along those lines, with trying to get the value of the button, but I messed it up somehow, so I thought it didn't work with buttons at all.

Thanks again.

<?php
$s=array('AAA','BBB','CCC','DDD','EEE','FFF','GGG','HHH','III','JJJ','KKKK','...','ZZZ');  $nr=count($s);
if (isset($_POST)) {
if (isset($_POST))
{
$position=array_search($_POST,$s);
$c1=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+1; }
$c2=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+2; }
$c3=$s[$position];
}
if (isset($_POST))
{
$position=array_search($_POST,$s);
$c1=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+1; }
$c2=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+2; }
$c3=$s[$position];
}
if (isset($_POST)) {
$position=array_search($_POST,$s);
$c1=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+1; }
$c2=$s[$position];
if ($position==$nr-1) { $position=0; } else { $position=$position+2; }
$c3=$s[$position];
}
} else {
$c1='AAA';
$c2='BBB';
$c3='CCC';
}
?>
<form name="form1" action="reload.php" method="post">
<table width="50%" border="1" cellspacing="0" cellpadding="1">
<tr>
<td align="center"><?php echo $c1; ?><input name="c1" type="hidden" value="<?php echo $c1; ?>"></td>
<td align="center"><?php echo $c2; ?><input name="c2" type="hidden" value="<?php echo $c2; ?>"></td>
<td align="center"><?php echo $c3; ?><input name="c3" type="hidden" value="<?php echo $c3; ?>"></td>
</tr>
<tr>
<td align="center"><input name="ONE" type="submit" id="ONE" value="ONE"></td>
<td align="center"><input name="TWO" type="submit" id="TWO" value="TWO"></td>
<td align="center"><input name="THREE" type="submit" id="THREE" value="THREE">
<input name="posted" type="hidden" value="posted"></td>
</tr>
</table>
</form>

Edited 3 Years Ago by happygeek: fixed formatting

LOL yeah. I tried to do it for myself afterwards, and ran into some troubles again. I'm probably going to have to take a few peeks at my homework to figure it out. :lol:

This article has been dead for over six months. Start a new discussion instead.