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>
<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> </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.
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.
Ouput:
Edit: Actually, I see nothing wrong with your code?
Output only clicking one:
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:
Set both buttons' name attribute to the same value.
Give each button a different value attribute, like so:
Then you can use an if statement in your php, like so:
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.
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: