1

I have a HTML / Twig / Bootstrap file with two buttons:

<form method="post" action="procuser.php">
    <div class="form-group">
        <label for="Details">Details:</label>
        <textarea class="form-control" rows="5" id="details" name="details">{{ details }}</textarea>
    </div>
    <button type="submit" name="button" value="submit" class="btn btn-default">Submit</button>
    <button type="submit" name="button" value="cancel" class="btn btn-default">Cancel</button
    <br><span class="err">{{ err }}</span>
</form>

Then in PHP, I check whether the form was submitted / cancelled using the checks:

if ($_SERVER["REQUEST_METHOD"] == "POST")
{
    if (isset($_GET["button"]) && $_GET["button"] == "submit")
    {
        if (postData(input("details"))) // <- never reaches here?
            header("success.php");
    }
    else
        header("menu.php");
}        

But the submit button does not work and I always get redirected to the menu.

gornvix
  • 3,154
  • 6
  • 35
  • 74
  • I would make to different forms, one with user input and submit button and other with cancel buttom – aiko Apr 14 '16 at 21:38
  • 1
    Your form method is `POST` but you check for `$_GET["button"]` – fusion3k Apr 14 '16 at 21:38
  • 1
    I don't anything about `twig` but I do know you're submitting the form with `POST` and try to get the values with `GET`. That's not gonna work. Try `$_POST[...]` instead. – icecub Apr 14 '16 at 21:39
  • @fusion3k why don't you post that as an answer...? Looks like the issue here... – benomatis Apr 14 '16 at 21:42
  • @icecub why don't you post that as an answer...? Looks like the issue here... – benomatis Apr 14 '16 at 21:42
  • @devlincarnate actually, default form method is [GET](http://stackoverflow.com/a/2314441/3294262) and button of type "submit" [are posted](https://www.w3.org/TR/html-markup/button.submit.html) – fusion3k Apr 14 '16 at 21:43
  • @webeno Because in this case I know it's causing an issue but I'm uncertain it will solve the problem completely. Aside from that, such simple problems are much faster answered with a comment and can still be turned into an answer if needed later on. – icecub Apr 14 '16 at 21:45
  • Yes, $_POST works fine (I'm not sure what devlin is saying). My PHP is rusty :( – gornvix Apr 14 '16 at 21:45
  • @tyebillion try replacing your `$_GET` with `$_POST` in your PHP and see if it makes a difference, then let us know by editing your original question pointing out what you edited for clarity (and especially as there is already an answer on it)... – benomatis Apr 14 '16 at 21:52

3 Answers3

2

According to this website: http://www.w3schools.com/tags/tag_button.asp

Note: If you use the <button> element in an HTML form, different browsers may submit different values. Use <input> to create buttons in an HTML form.

I recommend using

<input type="submit" name="button" value="submit" class="btn btn-default" />
<input type="submit" name="button" value="cancel" class="btn btn-default" />
Russell Hankins
  • 1,196
  • 9
  • 17
2

Because you used method="post" for form in html. That's reason you can't get value of the button by $_GET

Change:

if (isset($_GET["button"]) && $_GET["button"] == "submit")

to:

if (isset($_POST["button"]) && $_POST["button"] == "submit")
Duy Trần
  • 204
  • 3
  • 15
-1

if (isset($_GET["button"]) && $_GET["button"] == "submit")

in this code the event is not correct the user can click one button at a time so not possible to click two button at a time :)

use this

if (isset($_GET["button"]) = "submit")