How can I detect which submit button was pressed? - php

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.
<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!
)

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;
});

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)
);

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.

Related

combine all 3 $_post to 1 variable

JSFiddle
I wanted to combine all 3 $_POST to 1 variable but the month day wont show up, how come?
Here is the code:
<div id="register-birth-date">
<input type="number" name="register-day" placeholder="DATE">
<select id="register-month">
<option value="00">Mouth</option>
<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>
<input type="number" name=" " placeholder="YEARS">
</div>
$air_user_registered_date = $_POST['register-day']."/".$_POST['register-month']."/".$_POST['register-years'];
You need to add name attribute to both your select box for months and input box for years.
<div id="register-birth-date">
<input type="number" name="register-day" placeholder="DATE" />
<select name="register-month" id="register-month">
<option value="00">Mouth</option>
<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>
<input type="number" name="register-years" placeholder="YEARS" />
</div>
JSFiddle
Date Text box given the name of "register-day".
Month Select box give the name of "register-month".
Year Text box give name of "register-years".

PHP POST data not coming through with enctype="multipart/form-data"

I'm having trouble getting my form's post data to come through. As this code stands, the file uploads, but I don't have any post data for the other form fields. I noticed that if I get rid of multipart/form-data enctype, it will not upload (obviously) but I DO get the post data to come through for the other fields. Any ideas on why this is?
The page where this code occurs is index.php?page=page1&upload=1
<form enctype="multipart/form-data" action="?page=page1&upload=2" method="post">
Select Location: <select name="location">
<option>Select Location</option>
<option value="N">North</option>
<option value="S">South</option>
<option value="E">East</option>
<option value="W">West</option>
</select><br />
Upload File: <input name="ufile" type="file" id="ufile" size="50" /><br />
<b><u>Sunday</u></b> Date: <select name="month">
<option> - Month - </option>
<option value="01">January</option>
<option value="02">Febuary</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="day">
<option> - Day - </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>
<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="year">
<?php
$optionyear = date("Y",time());
$currentyear = $optionyear;
while($optionyear != 1990) {
echo "<option value=\"".$optionyear."\"";
if($optionyear == $currentyear) {
echo " selected";
}
echo ">".$optionyear."</option>";
$optionyear--;
}
?>
</select><br />
<input type="submit" name="Submit" value="Upload" /><br />
</form>
Thanks!
U can get your upload data with $_FILES[] and other data with $_POST[]
Try
echo "<pre>";
print_r($_POST);
print_r($_FILES['ufile']);
echo "</pre>";
By the way where is close tag for <form> element?

I have several dropdown list - I want to print out selected options of these dropdown lists

This is what I got so far, it's just the dropdown list. It's a form for making a reservation for appointment. I want to print out these selected values (= selected options of dropdown)
But I have no clue since I'm very new with PHP and HTML. Something with $_POST and $_GET ? Could I have some suggestions of solution? Much gratitude.
<html>
<head>
<title>title</title>
</head>
<body>
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</body>
</html>
You're missing the <form> tag. It should be wrapped around all your form-data (select, input) etc. Then you will fetch it using either $_GET or $_POST or even $_REQUIRE
Example (change site.php to whatever you please):
<html>
<head>
<title>title</title>
</head>
<body>
<form method="post" action="site.php">
<select name="Ar" id="Ar">
<option value="1">2014</option>
<option value="2">2015</option>
<option value="3">2016</option>
<option value="4">2017</option>
<option value="5">2018</option>
<option value="6">2019</option>
</select>
<select name="Manad" id="Manad">
<option value="1">Januari</option>
<option value="2">Februari</option>
<option value="3">Mars</option>
<option value="4">April</option>
<option value="5">Maj</option>
<option value="6">Juni</option>
<option value="7">July</option>
<option value="8">Augusti</option>
<option value="9">September</option>
<option value="10">Oktober</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="Dag" id="Dag">
<option value="1">Mandag</option>
<option value="2">Tisdag</option>
<option value="3">Onsdag</option>
<option value="4">Torsdag</option>
<option value="5">Fredag</option>
</select>
<select name="Tid" id="Tid">
<option value="1">07:00</option>
<option value="2">08:00</option>
<option value="3">09:00</option>
<option value="4">10:00</option>
<option value="5">11:00</option>
<option value="6">12:00</option>
<option value="7">13:00</option>
<option value="8">14:00</option>
<option value="9">15:00</option>
<option value="10">16:00</option>
<option value="11">17:00</option>
<option value="12">18:00</option>
</select>
<input type="submit" name="submit" id="submit" value="Boka" />
</form>
</body>
</html>
PHP example:
if(isset($_POST['Ar'])) { // Make sure it's sent
$year = $_POST['Ar'];
echo "År: " . htmlentities($year); // Output (htmlentities for safety when outputting)
$month = $_POST['Manad'];
echo "Månad: " . htmlentities($month);
}

How do I limit form options based on previous entry & leave all fields (including file selection) populated on submittion

I have the current form:
<form name="estimate" action="live_preview.php" method="post"enctype="multipart/form-data">
<label for="file">Upload File 1:</label>
<input type="file" name="file" id="file"/>
<label for="qty1">Quantity:</label>
<select name="qty1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<label for="file">Upload File 2:</label>
<input type="file" name="file2" id="file2"/>
<label for="qty2">Quantity:</label>
<select name="qty2">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<label for="file">Upload File 3:</label>
<input type="file" name="file3" id="file3"/>
<label for="qty3">Quantity:</label>
<select name="qty3">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<label for="file">Upload File 4:</label>
<input type="file" name="file4" id="file4"/>
<label for="qty4">Quantity:</label>
<select name="qty4">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<label for="file">Upload File 5:</label>
<input type="file" name="file5" id="file5"/>
<label for="qty5">Quantity:</label>
<select name="qty5">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
</select>
<br>
<br>
<label for="meterial">Meterial:</label>
<select name="material">
<option value="PLA">PLA (0.60$ / Gram)</option>
<option value="ABS">ABS (0.60$ / Gram)</option>
<option value="POLYCARB">PolyCarb (1.80$ / Gram)</option>
<option value="NYLON">Nylon (1.20$ / Gram)</option>
</select>
|
<label for="color">Color:</label>
<select name="color">
<option value="WHITE">White</option>
<option value="BLACK">Black</option>
<option value="RED">Red</option>
<option value="GREEN">Green</option>
<option value="GRAY">Gray</option>
</select>
|
<label for="delivery">Delivery Method:</label>
<select name="delivery">
<option value="PICK UP">Pick Up (Free)</option>
<option value="SHIPPED">Shipped (+10$)</option>
</select>
|
<label for="marketing">Marketing?</label>
<select name="marketing">
<option value="YES">YES</option>
<option value="NO">NO</option>
</select>
<br>
<label for="infill">Infill: 20%</label>
<input type="range" name="infill" id="infill" value="20" min="20" max="90" step="10" onchange="printValue('infill','rangeValue1')"/>
90%
<input id="rangeValue1" type="text" size="2"/>
<br>
<ul class="buttons vertical">
<input type="submit" class="button fit scrolly" name="submit" value="Refresh Estimate" />
</ul>
</form>
What I'm trying to do is:
Being able to limit the colour selection based on the type of material previously chosen
Upon submission, process php file while still keeping all fields populated so that if user isn't happy with price given, they can modify some settings and get a new "estimate"
have the following actions happen w/o a page refresh (not a priority)
Thank you very much for taking the time to read this! Its extremely appreciated.
1.
This would be achieved with client-side code. Javascript / jQuery is what you need here.
2.
Unfortunately, file fields can't be kept populated when you reload the page (or redirect back). You can keep them populated by using AJAX to send your form contents, but it can be quite complex and sending files with AJAX will only work in modern browsers.
3.
AJAX is what you are looking for if you want server actions to happen without a page refresh. Look at jQuery $.post. My suggestion is that you have your form which people begin to fill out. The site will check the inputs along the way and suggest prices without refreshing the page, and once they are happy and ready to go, they submit the form which will upload the files and "refresh the page".
on 2, php only solution: You can keep the fields populated on submission by adding a placeholder to the input field. The placeholder gets the submitted value ($_POST('value') or similar). Just make sure that the value isn't lost if you have several fields and thus several submits before the customer hits the final "all fine" button, maybe use $_SESSION for that.

dynamically adding sets of form elements n times

i'm working on an application that lets users enter the opening hours for a restaurant. i have the form code set up like this:
<div class="hourswrapper">
Days:
<input type="checkbox" name="day1[]" value="1" />M
<input type="checkbox" name="day1[]" value="2" />Tu
<input type="checkbox" name="day1[]" value="3" />W
<input type="checkbox" name="day1[]" value="4" />Th
<input type="checkbox" name="day1[]" value="5" />F
<input type="checkbox" name="day1[]" value="6" />Sa
<input type="checkbox" name="day1[]" value="7" />Su
<br />
Opening Time:
<select name="starthour1">
<option value="12">12</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>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</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>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
so basically the person chooses a set of hours and marks which days those apply to. of course there might be multiple sets of hours (ex. if a restaurant is open 12-2 one day but 12-5 another). i'd like to have a "Add More Hours" button that duplicates this same code when pressed, however many times is necessary.
what's the best way to go about this? how do you dynamically add a form element without needing to have it hidden at the beginning? how do i set different numbered names for each set of hours inputs, and later know how many to access (using PHP post)? i assume i'll need a javascript function that keeps track of how many have been added.
alternatively, i am open to suggestions on how to present hours inputs differently/better, perhaps a way that doesn't require dynamic creation of form elements. i do want to restrict the user's inputs so i don't have to worry about parsing weird entries, which is why i don't want to just have an text input box for each day.
thanks.
Checkout http://www.w3schools.com/dom/dom_node.asp
Particularly the cloneNode and appendChild methods
Probably best to use JavaScript/JQuery to dynamically add new elements, then use foreach in your PHP script to process all the inputs?
Hope this helps.
Y.J.
Here's a quick little example of duplicating the hours input based on which checkbox is checked. Each hours div gets its' own unique id (see the attr('id',hrsDivID) part of the code below). Mimic this to rename your inputs/selects. See http://jquery.com/ and http://api.jquery.com/ for more.
<head>
<script type="text/javascript" src="/shared/javascript/jquery.js"></script>
</head>
<div id="hours" style='display:none'>
Opening Time:
<select name="starthour1">
<option value="12">12</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>
</select> :
<select name="startminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="startwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
Closing Time:
<select name="endhour1">
<option value="12">12</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>
</select> :
<select name="endminute1">
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
<option value="59">59</option>
</select>
<select name="endwhen1">
<option value="am">am</option>
<option value="pm">pm</option>
</select><br />
<input type="checkbox" name="24hours[]" value="yes" />Open 24 Hours
</div>
Days:
<div id='day1'>
<input type="checkbox" name="day1[]" value="1" onClick="addHours('day1');" >M
</div>
<div id='day2'>
<input type="checkbox" name="day1[]" value="2" onClick="addHours('day2');" />Tu
</div>
<div id='day3'>
<input type="checkbox" name="day1[]" value="3" onClick="addHours('day3');" />W
</div>
<div id='day4'>
<input type="checkbox" name="day1[]" value="4" onClick="addHours('day4');" />Th
</div>
<div id='day5'>
<input type="checkbox" name="day1[]" value="5" onClick="addHours('day5');" />F
</div>
<div id='day6'>
<input type="checkbox" name="day1[]" value="6" onClick="addHours('day6');"/>Sa
</div>
<div id='day7'>
<input type="checkbox" name="day1[]" value="7" onClick="addHours('day7');"/>Su
</div>
<script>
function addHours(divID){
var d = document.getElementById(divID);
var hrsDivID = divID+'_hours_div';
$('#hours').clone().attr('id',hrsDivID).css('display','').appendTo(d);
return false;
}
</script>

Categories