Using Submit buttons to invoke PHP actions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ExpertAlmost
Forum Commoner
Posts: 41
Joined: Mon Oct 20, 2008 12:26 am

Using Submit buttons to invoke PHP actions

Post 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";
?>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using NEXT and PREVIOUS buttons to invoke PHP actions

Post 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.
ExpertAlmost
Forum Commoner
Posts: 41
Joined: Mon Oct 20, 2008 12:26 am

Re: Using Submit buttons to invoke PHP actions

Post by ExpertAlmost »

That works great! Much neater. Thank you :)
Post Reply