1

In brief: is it possible to have multiple submit buttons on a page, and to detect on the receiving page which submit button was clicked?

For example, imagine a page that displays a random movie poster. There are two buttons: "I liked it", "I hated it." When either button is clicked, the form is submitted via POST to itself, where the user's like/dislike is noted in the database before displaying a new random poster.

My instinct was to write

 <form action="thispage.php" method="post">
      <input type="submit" name="like" value="I liked this." />
      <input type="submit" name="dislike" value="I hated this." />
 </form>

And then, on the receiving page,

if ($_POST['like'] == 1) ...

But that didn't work. Nor did

if ($_POST['submit'] == "like") ...

So I'm not sure what to do. Can anyone offer me a tip?

0

3 Answers 3

3

Option one is the typical beginners way. You check if your POST array exists, then you check the stored value if it does exist. Checking if it exists, and checking the exact value prevents output on initial page load.

<?php
if(isset($_POST['action']) && $_POST['action']  == 'like') {
echo "You like it!";
} elseif (isset($_POST['action']) && $_POST['action'] == 'hate' {
echo "You hate it :(";
}
?>

<form action="thispage.php" method="post">
  <input type="submit" name="action" value="like" />
  <input type="submit" name="action" value="hate" />
</form>

OR.... The switch/case allows you to run a number of predetermined 'answers' against the value of the POST['action'] var. You can also have a default value if none of the conditions are met. Read more here: http://www.php.net/manual/en/control-structures.switch.php

<?php
switch($_POST['action']) {

case "like":
echo "You like it";
break;

case "hate":
echo "You hate it";
break;

}

?>

<form action="thispage.php" method="post">
  <input type="submit" name="action" value="like" />
  <input type="submit" name="action" value="hate" />
</form>
Sign up to request clarification or add additional context in comments.

2 Comments

This person is brand new, you really need to explain what you're doing. That's a minimum for StackOverflow answers anyway.
Sorry, this is my first answer on here.. I updated my answer.
1

Yes, it is possible. You have the right idea. However, your checks are wrong in your if statements. Rather than checking for a specific value, since the submit buttons have different names you can simply check for their presence in the POST data.

if (isset($_POST['like'])) {

If you wanted to check for the specific value, you would use something like:

if (isset($_POST['like']) && $_POST['like'] === 'I liked this.') {

As you are just learning, I recommend getting familiar with debugging techniques. The easiest in this case to verify the data you get from the form would be to use print_r($_POST).

1 Comment

hmmm.. both input will be submitted in the form, be them clicked or not.. no? ------ so just tested and no, they don't, sorry, and thanks for having me know
0

Try this

<form action="" method="post">
  <input type="submit" name="like" value="I liked this." />
  <input type="submit" name="dislike" value="I hated this." />
</form>



<?php

if(isset($_POST['like']) ){
    echo "User says:  ". $_POST['like'];
    }


if(isset($_POST['dislike']) ){
    echo "User says:  ". $_POST['dislike'];
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.