Page 1 of 1

Using Submit buttons to invoke PHP actions

Posted: Wed Nov 19, 2008 10:17 pm
by ExpertAlmost
I've been reading and playing with forms and submit buttons. What I would like to do is have "next" and "previous" submit buttons and then POST which button the user clicked. But without user input. Is there a better way to do this besides using 2 forms and two submit buttons for next and previous? The code below works, but it seems kludgy. Is there a more "standard" way to do this? Also, how would I get the two buttons to appear next to each other?

Thank you for any pointers and cleverness!

HTML Side

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<div>
<form action="But2.php" method="post">
<INPUT TYPE="hidden" name="button" value="next">
<input type="submit" value="Next">
</form>
<form action="But2.php" method="post">
<input type="hidden" name="button" value="previous">
<input type="submit" value="Previous">
</form>
</div>
</body>
</html>
PHP Side:

Code: Select all

<?php
if ($_REQUEST["button"] == "next") print "in next</b><br/>\n\n";
if ($_REQUEST['button'] == "previous") print "in previous</b><br/>\n\n";
?>

Re: Using NEXT and PREVIOUS buttons to invoke PHP actions

Posted: Thu Nov 20, 2008 12:49 am
by requinix
Why not one form?

Code: Select all

<form action="target.php" method="post">
<input type="submit" name="previous" value="Previous">
<input type="submit" name="next" value="Next">
</form>
Either $_POST[previous] or $_POST[next] will be set.

I don't get the "without user input" part... you mean submit the form automatically? With Previous and Next buttons that doesn't make sense.

Re: Using Submit buttons to invoke PHP actions

Posted: Thu Nov 20, 2008 2:54 am
by ExpertAlmost
That works great! Much neater. Thank you :)