My form won't work - php

This form works well in choosing a season and an episode with my php handling script:
<form action="view" method="get">
<font color="white">Season:</font><select name="s">
<option id="s" value="1">1</option>
</select>
<font color="white">Episode: </font><select name="e">
<option id="e" value="1">1</option>
<option id="e" value="2">2</option>
<option id="e" value="3">3</option>
<option id="e" value="4">4</option>
<option id="e" value="5">5</option>
<option id="e" value="6">6</option>
<option id="e" value="7">7</option>
<option id="e" value="8">8</option>
<option id="e" value="9">9</option>
<option id="e" value="10">10</option>
<option id="e" value="11">11</option>
<option id="e" value="12">12</option>
<option id="e" value="13">13</option>
<option id="e" value="14">14</option>
<option id="e" value="15">15</option>
<option id="e" value="16">16</option>
<option id="e" value="17">17</option>
<option id="e" value="18">18</option>
<option id="e" value="19">19</option>
<option id="e" value="20">20</option>
<option id="e" value="21">21</option>
<option id="e" value="22">22</option>
<option id="e" value="23">23</option>
<option id="e" value="24">24</option>
</select>
<input type="submit" value="GO">
</form>
So, can anyone tell me why this form won't work to pick a random episode?
<form action="view" method="get">
<input type="hidden" id="s" value="1">
<input type="hidden" id="e" value="<?php echo(rand(1,24)); ?>">
<input type="submit" value="Watch Random Episode">
</form>
It won't append the variables to the end of the url, all I get is this:
http://127.0.0.1/media/show/view/?

The hidden inputs need a name attribute. In the same way that the select elements have a name..
<input type="hidden" name="s" id="s" value="1">
<input type="hidden" name="e" id="e" value="<?php echo(rand(1,24)); ?>">

Try something like this :
<?php
$ep = rand(1,24);
echo $ep;
?>
<form action="view" method="get">
<input type="hidden" id="s" value="1">
<input type="hidden" id="e" name="e" value="<?php echo $ep; ?>">
<input type="submit" value="Watch Random Episode">
</form>

First you need to define name attribute to each field and one space to echo and print value
<input type="hidden" id="s" value="1" name ="s">
<input type="hidden" id="e" name = "e" value="<?php echo rand(1,24); ?>">
Second check you have htaccess [QSA] also with rules

Related

My html form won't POST to mysql

Hello i have a problem where my data isn't being put into my MYSQL database.
Also there isn't any errors at all on screen or in the php error page.
My code is below.
This is my array with all the data in
$time_submit = date('d/m/Y h:i:s a', time());
$submit_data = array(
'time_submit' => $time_submit,
'time_event' => $_POST['date'] . " " . $_POST['hr'] . ":" . $_POST['min'],
'project_number' => $_POST['project'],
'event' => $_POST['event'],
'happened' => $_POST['happened'],
'about_it' => $_POST['about_it'],
'reporter_name' => $_POST['reporter'],
'organisation' => $_POST['organisation'],
'trackside' => $_POST['trackside'],
'event_location' => $_POST['location'],
'phone_number' => $_POST['number'],
'yes_rules' => $_POST['rules'],
'life_rules' => $_POST['life_rules'],
'require_feedback' => $_POST['feedback'],
'reporter_email' => $_POST['email'],
'region' => $_POST['region'],
'photo' => $file_path
);
submit_form($submit_data);
I have a function that submits the data
function submit_form($submit_data) {
global $conn;
array_walk($submit_data, 'array_sanitize');
$fields = '`' . implode('`, `', array_keys($submit_data)) . '`';
$data = '\'' . implode('\', \'', $submit_data) . '\'';
mysqli_query($conn,"INSERT INTO tb_data($fields) VALUES($data)");
}
I have echoed everything to make sure its all okay and everything looks fine.
My form
<form id="closecall" action="<? echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post" enctype="multipart/form-data">
<ul class="form">
<li><label><i>* Fields marked with an asterix are required</i></label></li><br />
<? echo output_errors($errors); ?>
<li><label for="date"><i>* </i>Event Date and time:<i class="required" id="datepicker_info">Required</i></label></li>
<li>
<input id="datepicker" type="text" name="date" >
<select name="hr" id="hr">
<option value="">Hr</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</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="00">00</option>
</select>
<select name="min" id="min">
<option value="">Min</option>
<option value="00">00</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</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>
<option value="32">32</option>
<option value="33">33</option>
<option value="34">34</option>
<option value="35">35</option>
<option value="36">36</option>
<option value="37">37</option>
<option value="38">38</option>
<option value="39">39</option>
<option value="40">40</option>
<option value="41">41</option>
<option value="42">42</option>
<option value="43">43</option>
<option value="44">44</option>
<option value="45">45</option>
<option value="46">46</option>
<option value="47">47</option>
<option value="48">48</option>
<option value="49">49</option>
<option value="50">50</option>
<option value="51">51</option>
<option value="52">52</option>
<option value="53">53</option>
<option value="54">54</option>
<option value="55">55</option>
<option value="56">56</option>
<option value="57">57</option>
<option value="58">58</option>
<option value="59">59</option>
</select>
</li><br />
<li><label for="Project"><i>* </i>Involved Project:<i class="required" id="project_info">Required</i></label></li>
<li><input id="project" name="project" type="text"></li><br />
<li><label for="Event"><i>* </i>Describe the Event:<i class="required" id="event_info">Required</i></label></li>
<li><textarea class="textarea" id="event" name="event"></textarea></li><br />
<li><label for="happened"><i>* </i>What Could Have Happened:<i class="required" id="happened_info">Required</i></label></li>
<li><textarea class="textarea" id="happened" name="happened"></textarea></li><br />
<li><label for="about_it"><i>* </i>What were you able to do about it:<i class="required" id="about_info">Required</i></label></li>
<li><textarea class="textarea" id="about_it" name="about_it"></textarea></li><br />
<li><label for="reported">Reported By:</label></li>
<li><input id="reported" name="reporter" type="text"/></li><br />
<li><label for="organisation"><i>* </i>Organisation:<i class="required" id="organisation_info">Required</i></label></li>
<li><input id="organisation" name="organisation" type="text"></li><br />
<li><label for="trackside"><i>* </i>Trackside or Non-Trackside:</label></li>
<li>
<input type="radio" name="trackside" value="Trackside"><span>Trackside</span>
<input type="radio" name="trackside" value="Non-Trackside" checked><span>Non-Trackside including Civils and Aiports</span>
</li><br />
<li><label for="location">Other Location Info (Stand No./ELR/Mileage etc):</label></li>
<li class="optional"><span>(If Known)</span></li>
<li><input id="location" name="location" type="text"/></li><br />
<li><label for="number">Reporters Mobile Number:</label></li>
<li><input id="contact" name="number" type="text"/></li><br />
<li><label for="rules"><i>* </i>Potential Breach of Life Saving Rules? (Not Applicable to Airports):</label></li>
<li>
<input type="radio" name="rules" class="rules" value="Yes" ><span>Yes</span>
<input type="radio" name="rules" class="rules" value="No"checked><span>No</span>
</li><br />
<li class="liferules" style="display: none;"><label for="liferules"><i>* </i>Life Saving Rules (Not Applicable to Airports):<i class="required" id="rules_info">Required</i></label></li>
<li class="liferules" style="display: none;">
<select name="life_rules" id="liferules">
<option value="">Please Select!</option>
<option value="Working with electricity">Working with electricity</option>
<option value="Working with electricity-test before touch">Working with electricity-test before touch</option>
<option value="Always obey the speed limit and wear a seat belt.">Always obey the speed limit and wear a seat belt.</option>
<option value="Never use a hand-held or hands-free phone, or programme any other mobile device, while driving.">Never use a hand-held or hands-free phone, or programme any other mobile device, while driving.</option>
<option value="Working at height-without a harness">Working at height-without a harness</option>
<option value="Equipment fit for intended purpose">Equipment fit for intended purpose</option>
<option value="Under the influence of Drugs or Alcohol">Under the influence of Drugs or Alcohol</option>
<option value="Wasn't trained for the job at hand">Wasn't trained for the job at hand</option>
<option value="Entered exclusion zone(Without permission)">Entered exclusion zone(Without permission)</option>
<option value="Plans and Permits Weren't in place before Job">Plans and Permits Weren't in place before Job</option>
</select>
</li><br />
<li><label for="feedback"><i>* </i>Does the Reporter Require Close Call Feedback:</label></li>
<li>
<input type="radio" name="feedback" class="feedback" value="Yes" ><span>Yes</span>
<input type="radio" name="feedback" class="feedback" value="No" checked/><span>No</span>
</li><br />
<li class="email" style="display: none;"><label for="email"><i>* </i>Please enter your email address:<i class="required" id="feedback_info">Required</i></label></li>
<li class="email" style="display: none;"><input id="feedback" type="email" name="email"></li><br />
<li><label for="region"><i>* </i>Region:<i class="required" id="region_info">Required</i></label></li>
<li>
<select id="region" name="region">
<option value="">Please Select!</option>
<option value="Wales and Western">Wales and Western</option>
<option value="Central">Central</option>
<option value="Southern">Southern</option>
<option value="Airports">Airports</option>
</select>
</li><br />
<li><label><i>* Max 5MB Photos and PDF's only </i>Do you want to upload a file:</label></li>
<li>
<input type="radio" name="upload" value="Yes"><span>Yes</span>
<input type="radio" name="upload" value="No" checked/><span>No</span>
</li><br />
<li style="display: none;" class="fileload"><label for="image">File Upload:</label></li><br />
<li style="display: none;" class="fileload"><input id="file0" type="file" name="file1" ></li><br />
<li style="display: none;" class="fileload"><input name="button" type="button" value="Upload More"></li><br />
<li style="display: none;" class="fileshow"><input type="file" name="file2"></li>
<li style="display: none;" class="fileshow"><input type="file" name="file3"></li>
<li style="display: none;" class="fileshow"><input type="file" name="file4"></li>
<li style="display: none;" class="fileshow"><input type="file" name="file5"></li><br />
<li><i class="required" id="end_info">Please fill out required fields!</i></li>
<li><input id="submit" type="submit" value="Submit" ></li>
<li></li>
I know the way i do my time is wrong and i am going to change it.
Thanks
You should check for MySQL Error with mysqli_error
if (!mysql_query($conn, ...)) {
echo mysqli_error($conn);
}

Getting Option Value with PHP

Below is a snippet of the code I have created on a form page. The form inputs display the correct variables for $adjustedGrossIncome, $personalExemptions and the calculation for Tax Due Before Refundable Credits, but when I try to echo $birthYear the php dies. Any hints what I'm doing wrong?
<?php
$filingStatus = $_Post['filingStatus'];
$birthYear = $_Post['birthYear'];
$dependents = $_Post['dependents'];
$children = $_POST['children'];
$exemptions = $_POST['exemptions'];
$income = $_POST['income'];
$investments = $_POST['investments'];
$retirement = $_POST['retirement'];
$property = $_POST['property'];
$adjustedGrossIncome = $income + $investments + $retirement;
$personalExemptions = ($exemptions * 3950);
?>
<form id="form1" name="form1" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset style="width:500px;">
<legend>Filing Status</legend>
<p>
<label>
<input type="radio" name="filingStatus" value="married" id="filingStatus" />
Married filing jointly</label>
<br />
<label>
<input type="radio" name="filingStatus" value="single" id="filingStatus" />
Single or Head of Household</label>
</p>
</fieldset>
<p>
<label>Birth Year
<select name="birthYear" id="birthYear">
<option>Select Year</option>
<option value="1923">1923</option>
<option value="1924">1924</option>
<option value="1925">1925</option>
<option value="1926">1926</option>
<option value="1927">1927</option>
<option value="1928">1928</option>
<option value="1929">1929</option>
<option value="1930">1930</option>
<option value="1931">1931</option>
<option value="1932">1932</option>
<option value="1933">1933</option>
<option value="1934">1934</option>
<option value="1935">1935</option>
<option value="1936">1936</option>
<option value="1937">1937</option>
<option value="1938">1938</option>
<option value="1939">1939</option>
<option value="1940">1940</option>
<option value="1941">1941</option>
<option value="1942">1942</option>
<option value="1943">1943</option>
<option value="1944">1944</option>
<option value="1945">1945</option>
<option value="1946">1946</option>
<option value="1947">1947</option>
<option value="1948">1948</option>
<option value="1949">1949</option>
<option value="1950">1950</option>
<option value="1951">1951</option>
<option value="1952">1952</option>
<option value="1953">1953</option>
<option value="1954">1954</option>
<option value="1955">1955</option>
<option value="1956">1956</option>
<option value="1957">1957</option>
<option value="1958">1958</option>
<option value="1959">1959</option>
<option value="1960">1960</option>
<option value="1961">1961</option>
<option value="1962">1962</option>
<option value="1963">1963</option>
<option value="1964">1964</option>
<option value="1965">1965</option>
<option value="1966">1966</option>
<option value="1967">1967</option>
<option value="1968">1968</option>
<option value="1969">1969</option>
<option value="1970">1970</option>
<option value="1971">1971</option>
<option value="1972">1972</option>
<option value="1973">1973</option>
<option value="1974">1974</option>
<option value="1975">1975</option>
<option value="1976">1976</option>
<option value="1977">1977</option>
<option value="1978">1978</option>
<option value="1979">1979</option>
<option value="1980">1980</option>
<option value="1981">1981</option>
<option value="1982">1982</option>
<option value="1983">1983</option>
<option value="1984">1984</option>
<option value="1985">1985</option>
<option value="1986">1986</option>
<option value="1987">1987</option>
<option value="1988">1988</option>
<option value="1989">1989</option>
<option value="1990">1990</option>
<option value="1991">1991</option>
<option value="1992">1992</option>
</select>
</label>
</p>
<p>
<label>Number of Dependents (not including taxpayer and spouse if applicable
<input type="text" name="dependents" id="dependents" />
</label>
</p>
<p>
<label>Number of Children under 18
<input type="text" name="children" id="children" />
</label>
</p>
<p>
<label>Total number of exemptions
<input type="text" name="exemptions" id="exemptions" />
</label>
</p>
<p>
<label>Total Wages/Salary
<input type="text" name="income" id="income" />
</label>
</p>
<p>
<label>Investment income (interest, dividends, capital gains)
<input type="text" name="investments" id="investments" />
</label>
</p>
<p>
<label>Retirement income (pension, IRA, 401(k), etc.)
<input type="text" name="retirement" id="retirement" />
</label>
</p>
<p>
<label>Annual property taxes paid on primary residence and/or annual rent
<input type="text" name="property" id="property" />
</label>
</p>
<p>
<input type="submit" name="submit" id="submit" value="Calculate Net Tax Liability for 2013" />
<input type="reset" name="reset" id="reset" value="Reset Form" onClick="window.location.reload()" />
</p>
</form>
<p>Adjusted Gross Income: $<?php echo($adjustedGrossIncome); ?></p>
<p>Personal Exemptions: $<?php echo($personalExemptions); ?></p>
<p>Tax Due Before Refundable Credits: $<?php echo(($adjustedGrossIncome - $personalExemptions) * .0425); ?></p>
<p><?php echo($birthYear); ?></p>
Change all $_Post to uppercase $_POST
This is known as a superglobal and must be in uppercase.
More on superglobals can be found on the PHP.net Web site
http://php.net/manual/en/language.variables.superglobals.php

Update combobox fail

i'm trying to use comboboxes for quantity in Virtuemart instead of quantity text and update button.
When i have only one article in the cart it works perfectly, but when i have more than one, it's not working.
Here's the form code
<form action="<?php echo JRoute::_ ('index.php'); ?>" method="post" class="inline" name="frm">
<input type="hidden" name="option" value="com_virtuemart"/>
<input type="text" title="<?php echo JText::_ ('COM_VIRTUEMART_CART_UPDATE') ?>.2" class="inputbox" size="3" maxlength="4" name="quantity" value="<?php echo $prow->quantity ?>" style="display:none;"/>
<select name="cantidad" id="cantidad" onchange="getval(this);" value="<?php echo $prow->quantity ?>" title="<?php echo JText::_ ('COM_VIRTUEMART_CART_UPDATE') ?>"> <script type="text/javascript">
function getval(sel) {
document.frm.quantity.value = (sel.value);
document.getElementById('actualizar').click();
}
</script>
<option value="<?php echo $prow->quantity ?>"><?php echo $prow->quantity ?></option>
<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>
<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>
</select>
<input type="hidden" name="view" value="cart"/>
<input type="hidden" name="task" value="update"/>
<input type="hidden" name="cart_virtuemart_product_id" value="<?php echo $prow->cart_item_id ?>"/>
<input type="submit" class="vmicon vm2-add_quantity_cart" id="actualizar" name="update" title="<?php echo JText::_ ('COM_VIRTUEMART_CART_UPDATE') ?>" align="middle" value=" " style="display:none;"/>
</form>
Thank you

How can I detect which submit button was pressed?

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.

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