0

I have a script in PHP where I need to detect which button was pressed in a HTML page. I have found lots of similar questions asked by other people, but I am having problems.

HTML

<form method="post" action="kode/kode_newsletter_operation.php" id="newsletter_step3Form">
                    <p>
                        <label>Schedule newsletter:</label>
                        <select name="send_date_month" id="send_date_month">
                            <option value="01">January</option>
                            <option value="02">February</option>
                            <option value="03">March</option>
                            <option value="04">April</option>
                            <option value="05">May</option>
                            <option value="06">June</option>
                            <option value="07">July</option>
                            <option value="08">August</option>
                            <option value="09">September</option>
                            <option value="10">October</option>
                            <option value="11">November</option>
                            <option value="12">December</option>
                        </select>
                        -
                        <select name="send_date_day" id="send_date_day">
                            <option value="01">1</option>
                            <option value="02">2</option>
                            <option value="03">3</option>
                            <option value="04">4</option>
                            <option value="05">5</option>
                            <option value="06">6</option>
                            <option value="07">7</option>
                            <option value="08">8</option>
                            <option value="09">9</option>
                            <option value="10">10</option>
                            <option value="11">11</option>
                            <option value="12">12</option>
                            <option value="13">13</option>
                            <option value="14">14</option>
                            <option value="15">15</option>
                            <option value="16">16</option>
                            <option value="17">17</option>
                            <option value="18">18</option>
                            <option value="19">19</option>
                            <option value="20">20</option>
                            <option value="21">21</option>
                            <option value="22">22</option>
                            <option value="23">23</option>
                            <option value="24">24</option>
                            <option value="25">25</option>
                            <option value="26">26</option>
                            <option value="27">27</option>
                            <option value="28">28</option>
                            <option value="29">29</option>
                            <option value="30">30</option>
                            <option value="31">31</option>
                        </select>
                        -
                        <select name="send_date_year" id="send_date_year">
                            <option value='2012'>2012</option><option value='2013'>2013</option>                                </select>&nbsp;
                            <select name="send_date_hour" id="send_date_hour">
                                <option value="00">0</option>
                                <option value="01">1</option>
                                <option value="02">2</option>
                                <option value="03">3</option>
                                <option value="04">4</option>
                                <option value="05">5</option>
                                <option value="06">6</option>
                                <option value="07">7</option>
                                <option value="08">8</option>
                                <option value="09">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>
                                <option value="13">13</option>
                                <option value="14">14</option>
                                <option value="15">15</option>
                                <option value="16">16</option>
                                <option value="17">17</option>
                                <option value="18">18</option>
                                <option value="19">19</option>
                                <option value="20">20</option>
                                <option value="21">21</option>
                                <option value="22">22</option>
                                <option value="23">23</option>
                            </select>:<select name="send_date_min" id="send_date_min">
                                <option value="00">00</option>
                                <option value="00">05</option>
                                <option value="10">10</option>
                                <option value="00">15</option>
                                <option value="20">20</option>
                                <option value="00">25</option>
                                <option value="30">30</option>
                                <option value="00">35</option>
                                <option value="40">40</option>
                                <option value="00">45</option>
                                <option value="50">50</option>
                                <option value="00">55</option>
                            </select>
                            <div>Server Time: October 12, 2012 04:54</div>
                        </p>
                        <p>
                            <label for="send_date">Preview in Email:</label>
                            <input type="text" id="preview_email"  value="" name="preview_email" />
                            <input type="button" id="preview_btn" name="preview_btn" class="btn btn_red" onclick="previewSendMail(1)" value="Send Preview"/>
                        </p>
                        <p>&nbsp; </p>
                        <div id="kode_result"></div>
                        <div id="content_buttons">
                            <input type="button" class="btn btn_blue big" name="goback" value="Last Step" onclick="window.location.href='./newsletter_create_step2.php?id=1'" />
                            <input type="submit" name="submit_btn_now" value="Send now!" class=" btn btn_red big"/>
                            <input type="submit" name="submit_btn" value="Schedule" class=" btn btn_green big"/>
                            <input name="action" type="hidden" value="newsletter_step3" />
                            <input type="hidden" name="newsletter_id" id="newsletter_id" value="1"/>
                        </div>
                    </form>

PHP

print_r($_POST);

Output

Array(
[send_date_month] => 10
[send_date_day] => 12
[send_date_year] => 2012
[send_date_hour] => 01
[send_date_min] => 50
[preview_email] =>
[action] => newsletter_step3
[newsletter_id] => 1
)

Both buttons are inside the <form> tag but the $_POST array doesn't include any info about them. What am I doing wrong? Do the most recent browsers send no information about the submit button or am I missing something? I have no skills in javascript to develop a workaround for this, I only know PHP.

Thanks in advance.

9
  • 1
    Two separate forms are not needed. I've used two submit buttons in a single form hundreds of times. Commented Oct 11, 2012 at 23:21
  • 1
    I've added the whole form code to the question. Commented Oct 11, 2012 at 23:32
  • 1
    I've tried your code in my development environment and sorry to say but [submit_btn_now] or [submit_btn] return as expected in the $_POST array. What is your web server software and PHP version? scuzzy.id.au/stackoverflow/12850041.php Commented Oct 11, 2012 at 23:34
  • 1
    You can use firebug with firefox to review the post data sent in the request, I'd suggest looking at what the client is submitting. (Network tab, with "persist" enabled) Commented Oct 11, 2012 at 23:43
  • 1
    No he's right, try loading JQuery on your page and using some of the validation features. It captures the event, returns default, then submits it itself. Commented Oct 11, 2012 at 23:47

4 Answers 4

8

Give the submit buttons the same name attribute, but with different values, then you can test against the text that is used on the submit button.

<pre><? print_r($_POST);?></pre>
<form method="post">
<input type="submit" name="button" value="AAA">
<input type="submit" name="button" value="BBB">
</form>

Ouput:

Array
(
    [button] => AAA
)

Edit: Actually, I see nothing wrong with your code?

<pre><? print_r($_POST);?></pre>
<form method="post">
<input type="submit" name="submit_btn_now" value="Send now!" class=" btn btn_red big"/>
<input type="submit" name="submit_btn" value="Schedule" class=" btn btn_green big"/>
</form>

Output only clicking one:

Array
(
    [submit_btn_now] => Send now!
)
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, I put the code by @gtugablue into my test (obviously without knowing his <form> element) and honestly it worked as you'd expect, there must be some other underlying issue here.
what I'm thinking is he has nothing but the buttons, like your example, and GET as the method, which gives him an empty POST.
I'd assume this as well, you should post an answer about the method attribute of a form element and I'll upvote it.
3

Do you have JQuery or any other JS libraries running on your form? They tend to take over the submit event and thus the submit doesn't come from the actual submit input. Try it without a js library?

EDIT: Here is a work around:

$('#submit_btn_now').on('click', function() {
    var hiddensubmit=document.createElement('input');
    hiddensubmit.value = this.value;
    hiddensubmit.name = this.name;
    hiddensubmit.setAttribute('hidden','hidden');
    $('#newsletter_step3Form').append(hiddensubmit);
    $('#newsletter_step3Form').submit()
    return false;
});

4 Comments

I rather like this (just add an attribute type of hidden)
Actually, what if there is a "return false" as part of a validation routine that prevents the submit, maybe you should "clean up" dynamically added fields before creating new ones.
well it's a full fledged submit, so you won't see it, really, but alright. I'm not sure I see what you mean by cleaning up dynamically added fields, but yes if a handler is already passing false we have a problem.
Yes, that was exactly the problem. I had a headhache looking at the PHP and HTML scripts without detecting the problem but I didn't notice there was a javascript file taking over the submit event. Thanks for the replies.
1

If you want to use the same value for all buttons (as it is the value that is shown as text) you could use the name attribute as information holder and prefix it. The way to get the value is then:

$information = substr(
  end(
    array_filter(
      array_keys($_REQUEST),
      function ($key) { return substr($key, 0, strlen(PREFIX)) == PREFIX; }
    )
  ),
  strlen(PREFIX)
);

Comments

0

Set both buttons' name attribute to the same value.
Give each button a different value attribute, like so:

<input type="submit" name="submit" value="Log In">
<input type="submit" name="submit" value="Sign Up">

Then you can use an if statement in your php, like so:

if ($_POST['submit']=="Log In") {}
else {} // ELSE DETECTS THE "Sign Up" BUTTON, OR ANY OTHER BUTTON WITH A DIFFERENT VALUE

I think this pretty much gives you exactly the answer you were looking for: A way to detect which button was pressed using just PHP and no JavaScript.

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.