JQuery does not work but last element in all browsers - javascript

I am writing a web application for capturing applicant data. I am using JQuery for hiding and showing html tables. The code snippet is
<script type="text/javascript" src="jquery.min.js"></script>
<script language="javascript" type="text/javascript">
function hideDD(){
var gndr = $('#gender').val();
if(gndr=="Female"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var cat = $('#category').val();
if(cat=="SC" || cat=="ST"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var xsrv = $('#exsrv').val();
if(xsrv=="Yes"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var PD = $('#PWDPH').val();
if(PD=="Yes"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
}
and the html part is
<tr>
<td bgcolor="#FFF3D5">5. Gender :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="gender" id="gender" onclick="hideDD()">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
<!-- <div id="gender">
Please select whichever is applicable to you <select name="gender" id="gender">
<option value="">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</div> -->
</tr>
<tr>
<td bgcolor="#FFF3D5">6. Marital Status :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="maritalstatus" id="maritalstatus">
<option value="">Select</option>
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
<option value="Widow">Widow</option>
<option value="Divorced">Divorced</option>
<option value="Judicially separated and not married">Judicially separated and not married</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">7. Date of Birth: <span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><input name="dateofbirth" type="text" id="dateofbirth" value="<?php echo "$rs1->dateofbirth" ?>" readonly /><span class="style2">(yyyy/mm/dd)</span>
Your age(as on 31st July 2015):<input name="ageyear" type="text" id="ageyear" size="2" readonly>years<input name="agemonth" type="text" id="agemonth" size="2" readonly>months and<input name="ageday" type="text" id="ageday" size="2" readonly>days
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">8. Email:<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><input name="afc_email" type="text" id="afc_email" value="<?php echo "$rs1->email" ?>" readonly />
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">9. Category :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="category" id="category" onclick="hideDD()">
<option value="">Select</option>
<option value="General">General</option>
<option value="OBC">OBC</option>
<option value="SC">SC</option>
<option value="ST">ST</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">10 : You are a citizen of India by<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="citizen" id="citizen">
<option value="">Select</option>
<option value="Birth">Birth</option>
<option value="Domicile">Domicile</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">11 : Religion<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="religion" id="religion">
<option value="">Select</option>
<option value="Hindu">Hindu</option>
<option value="Muslims">Muslims</option>
<option value="Christians">Christians</option>
<option value="Sikhs">Sikhs</option>
<option value="Buddhists">Buddhists</option>
<option value="Zoroastrians">Zoroastrians</option>
<option value="Others">Others</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">12 : Whether Ex-servicemen<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="exsrv" id="exsrv" onclick="enb_dis(); hideDD()">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<table name="table_exserve" id="table_exserve" border="1">
<tr>
<td>Have you already secured employment in civil side under central Govt. in Group C and D in regular basis after availing this benefit</td>
<td bgcolor="#FFF3D5"><select name="exsrv_benefit" id="exsrv_benefit">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select> </td>
</tr>
<tr>
<td>Date of appoinment in arm force</td>
<td bgcolor="#FFF3D5"><input name="exsrv_from" type="text" id="exsrv_from" maxlength="10"/><span class="style2">(yyyy/mm/dd)</span></td>
</tr>
<tr>
<td>Date of discharge / due date of discharge from arm force</td>
<td bgcolor="#FFF3D5"><input name="exsrv_to" type="text" id="exsrv_to" maxlength="10" onchange="milical()"/><span class="style2">(yyyy/mm/dd)</span></td>
</tr>
<tr>
<td>Length of actual Military services</td>
<td bgcolor="#FFF3D5"><input name="miliyear" type="text" id="miliyear" size="2" readonly>years<input name="milimonth" type="text" id="milimonth" size="2" readonly>months and<input name="miliday" type="text" id="miliday" size="2" readonly>days</td>
</tr>
</table>
</tr>
<tr>
<td bgcolor="#FFF3D5">13 : Whether person with disability(PWD) / Physically handicapped (PH)<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><select name="PWDPH" id="PWDPH" onclick="enb_dis(); hideDD()">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<table name="table_PWDPH" id="table_PWDPH" border="1">
<tr>
<td>Nature of disability</td>
<td bgcolor="#FFF3D5"><select name="PWDPH_nature" id="PWDPH_nature">
<option value="">Select</option>
<option value="B">B</option>
<option value="LV">LV</option>
<option value="PD">PD</option>
<option value="D">D</option>
<option value="OA">OA</option>
<option value="OL">OL</option>
<option value="BL">BL</option>
</select> </td>
</tr>
<tr>
<td>Degree of disability</td>
<td bgcolor="#FFF3D5"><select name="PWDPH_degree" id="PWDPH_degree">
<option value="">Select</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>
<option value="60">60</option>
<option value="61">61</option>
<option value="62">62</option>
<option value="63">63</option>
<option value="64">64</option>
<option value="65">65</option>
<option value="66">66</option>
<option value="67">67</option>
<option value="68">68</option>
<option value="69">69</option>
<option value="70">70</option>
<option value="71">71</option>
<option value="72">72</option>
<option value="73">73</option>
<option value="74">74</option>
<option value="75">75</option>
<option value="76">76</option>
<option value="77">77</option>
<option value="78">78</option>
<option value="79">79</option>
<option value="80">80</option>
<option value="81">81</option>
<option value="82">82</option>
<option value="83">83</option>
<option value="84">84</option>
<option value="85">85</option>
<option value="86">86</option>
<option value="87">87</option>
<option value="88">88</option>
<option value="89">89</option>
<option value="90">90</option>
<option value="91">91</option>
<option value="92">92</option>
<option value="93">93</option>
<option value="94">94</option>
<option value="95">95</option>
<option value="96">96</option>
<option value="97">97</option>
<option value="98">98</option>
<option value="99">99</option>
<option value="100">100</option>
</select>
</td>
</tr>
</table>
</tr>
Every time I did modification, the last jQuery item worked fine, rests did not work. For example in this code only PWDPD element worked fine, category and gender did not worked fine.

I've made several changes in your code like <table> tag was not not implemented properly and without that you had <tr> at beginning. So after editing all those things your code goes as below:
I've kept an alert inside your function which shows up on each element you have called that function and I've change onclick to onchange for your select element.
function hideDD() {
var gndr = $('#gender').val();
alert(gndr);
if(gndr=="Female"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var cat = $('#category').val();
if(cat=="SC" || cat=="ST"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var xsrv = $('#exsrv').val();
if(xsrv=="Yes"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
var PD = $('#PWDPH').val();
if(PD=="Yes"){
$("#table_dd").hide();
$("#dd_bank").val('');
$("#dd_number").val('');
$("#dd_date").val('');
}
else
{
$("#table_dd").show();
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td bgcolor="#FFF3D5">5. Gender :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select id="gender" onchange="hideDD()">
<option value="Select">Select</option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">6. Marital Status :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="maritalstatus" id="maritalstatus">
<option value="">Select</option>
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
<option value="Widow">Widow</option>
<option value="Divorced">Divorced</option>
<option value="Judicially separated and not married">Judicially separated and not married</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">7. Date of Birth: <span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><input name="dateofbirth" type="text" id="dateofbirth" value="<?php echo "$rs1->dateofbirth" ?>" readonly />
<span class="style2">(yyyy/mm/dd)</span>
Your age(as on 31st July 2015):<input name="ageyear" type="text" id="ageyear" size="2" readonly>years<input name="agemonth" type="text" id="agemonth" size="2" readonly>months and<input name="ageday" type="text" id="ageday" size="2" readonly>days
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">8. Email:<span class="style2">*</span></td>
<td bgcolor="#FFF3D5"><input name="afc_email" type="text" id="afc_email" value="<?php echo "$rs1->email" ?>" readonly /></td>
</tr>
<tr>
<td bgcolor="#FFF3D5">9. Category :<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="category" id="category" onchange="hideDD()">
<option value="">Select</option>
<option value="General">General</option>
<option value="OBC">OBC</option>
<option value="SC">SC</option>
<option value="ST">ST</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">10 : You are a citizen of India by<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="citizen" id="citizen">
<option value="">Select</option>
<option value="Birth">Birth</option>
<option value="Domicile">Domicile</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">11 : Religion<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="religion" id="religion">
<option value="">Select</option>
<option value="Hindu">Hindu</option>
<option value="Muslims">Muslims</option>
<option value="Christians">Christians</option>
<option value="Sikhs">Sikhs</option>
<option value="Buddhists">Buddhists</option>
<option value="Zoroastrians">Zoroastrians</option>
<option value="Others">Others</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">12 : Whether Ex-servicemen<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="exsrv" id="exsrv" onchange="hideDD()">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<table name="table_exserve" id="table_exserve" border="1">
<tr>
<td>Have you already secured employment in civil side under central Govt. in Group C and D in regular basis after availing this benefit</td>
<td bgcolor="#FFF3D5">
<select name="exsrv_benefit" id="exsrv_benefit">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</td>
</tr>
<tr>
<td>Date of appoinment in arm force</td>
<td bgcolor="#FFF3D5"><input name="exsrv_from" type="text" id="exsrv_from" maxlength="10"/><span class="style2">(yyyy/mm/dd)</span></td>
</tr>
<tr>
<td>Date of discharge / due date of discharge from arm force</td>
<td bgcolor="#FFF3D5"><input name="exsrv_to" type="text" id="exsrv_to" maxlength="10" onchange="milical()"/><span class="style2">(yyyy/mm/dd)</span></td>
</tr>
<tr>
<td>Length of actual Military services</td>
<td bgcolor="#FFF3D5"><input name="miliyear" type="text" id="miliyear" size="2" readonly>years<input name="milimonth" type="text" id="milimonth" size="2" readonly>months and<input name="miliday" type="text" id="miliday" size="2" readonly>days</td>
</tr>
</table>
</td>
</tr>
<tr>
<td bgcolor="#FFF3D5">13 : Whether person with disability(PWD) / Physically handicapped (PH)<span class="style2">*</span></td>
<td bgcolor="#FFF3D5">
<select name="PWDPH" id="PWDPH" onchange="hideDD();">
<option value="">Select</option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
<table name="table_PWDPH" id="table_PWDPH" border="1">
<tr>
<td>Nature of disability</td>
<td bgcolor="#FFF3D5">
<select name="PWDPH_nature" id="PWDPH_nature">
<option value="">Select</option>
<option value="B">B</option>
<option value="LV">LV</option>
<option value="PD">PD</option>
<option value="D">D</option>
<option value="OA">OA</option>
<option value="OL">OL</option>
<option value="BL">BL</option>
</select>
</td>
</tr>
<tr>
<td>Degree of disability</td>
<td bgcolor="#FFF3D5">
<select name="PWDPH_degree" id="PWDPH_degree">
<option value="">Select</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>
<option value="60">60</option>
<option value="61">61</option>
<option value="62">62</option>
<option value="63">63</option>
<option value="64">64</option>
<option value="65">65</option>
<option value="66">66</option>
<option value="67">67</option>
<option value="68">68</option>
<option value="69">69</option>
<option value="70">70</option>
<option value="71">71</option>
<option value="72">72</option>
<option value="73">73</option>
<option value="74">74</option>
<option value="75">75</option>
<option value="76">76</option>
<option value="77">77</option>
<option value="78">78</option>
<option value="79">79</option>
<option value="80">80</option>
<option value="81">81</option>
<option value="82">82</option>
<option value="83">83</option>
<option value="84">84</option>
<option value="85">85</option>
<option value="86">86</option>
<option value="87">87</option>
<option value="88">88</option>
<option value="89">89</option>
<option value="90">90</option>
<option value="91">91</option>
<option value="92">92</option>
<option value="93">93</option>
<option value="94">94</option>
<option value="95">95</option>
<option value="96">96</option>
<option value="97">97</option>
<option value="98">98</option>
<option value="99">99</option>
<option value="100">100</option>
</select>
</td>
</tr>
</table>
</td>
</tr>
</table>

try place you code inside:
$(document).ready(function(){
YOUR CODE HERE
});

Related

Table with unique drop down combination for each row

I am having a table where there is two drop down drop-1 and drop-2. So I want to have drop-1 and drop-2 combination as unique.
I tried to create a new object and store that value in that but did not work.
<!DOCTYPE html>
<html>
<body>
<h2>Table</h2>
<table>
<tr>
<th>DD 1</th>
<th>DD 2</th>
</tr>
<tr>
<td><select name="D1">
<option value="fan">Fan</option>
<option value="car">Car</option>
<option value="cycle">Cycle</option>
<option value="truck">Truck</option>
</select></td>
<td><select name="D2">
<option value="electricity">Electricity</option>
<option value="petrol">Petrol</option>
<option value="tube">Tube</option>
<option value="load">Load</option>
</select>
</td>
</tr>
<tr>
<td><select name="D1">
<option value="fan">Fan</option>
<option value="car">Car</option>
<option value="cycle">Cycle</option>
<option value="truck">Truck</option>
</select></td>
<td><select name="D2">
<option value="electricity">Electricity</option>
<option value="petrol">Petrol</option>
<option value="tube">Tube</option>
<option value="load">Load</option>
</select>
</td>
</tr>
<tr>
<td><select name="D1">
<option value="fan">Fan</option>
<option value="car">Car</option>
<option value="cycle">Cycle</option>
<option value="truck">Truck</option>
</select></td>
<td><select name="D2">
<option value="electricity">Electricity</option>
<option value="petrol">Petrol</option>
<option value="tube">Tube</option>
<option value="load">Load</option>
</select>
</td>
</tr>
</table>
</body>
</html>
Need some clarity of thoughts how to achieve that need some suggestion the best possible way to do it if possible with an example

Using a cookie to store data

I'm in the process of creating a login and register page for an website. I decided to use a cookie to store the registered data rather than using PHP. How exactly I would do this? I have basic details like firstname, surname, birthday, username, email and password. I want to store the details so when the user registers with the site, they can then login with what they have registered with. This is all done in a form which is below:
<form action="" method="post" name="form1" onsubmit="getdropdownvalue()">
<table bgcolor="white" width="700" border="0" align="center">
<col width="200">
<col width="200">
<tr>
<th colspan="2" align="center" bgcolor="grey">Fill the form to sign up.</th>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2" style="font-size:12px">Fields marked with * are mandatory</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Name*</td>
<td>Surname*</td>
</tr>
<tr>
<td>
<label>
<input type="text" name="txtname" id="txtname" class="info" />
<td>
<label>
<input type="text" name="txtsurname" id="txtsurname" class="info" />
</label>
</td>
</tr>
<tr>
<td>Create Username*</td>
<td>Birthday</td>
</tr>
<tr>
<td>
<label>
<input type="text" name="txtusername" id="txtusername" class="info" />
</label>
</td>
<td>
<select name="DOBMonth">
<option>Month</option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay">
<option>Day</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>
<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="DOBYear">
<option>Year</option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1998</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select>
<tr>
<td>Email*</td>
<td>Confirm Email*</td>
</tr>
<tr>
<td>
<label>
<input type="text" name="txtemail" id="txtemail" class="info" onchange="ValidateEmail(txtemail)" />
<td>
<label>
<input type="text" name="txtemail2" id="txtemail2" class="info" onchange="ValidateEmail2(txtemail2)" />
</label>
</td>
</tr>
<tr>
<td>Password*</td>
<td>Confirm Pasword*</td>
</tr>
<tr>
<td>
<label>
<input type="password" name="txtpassword" id="txtpassword" class="info" />
<td>
<label>
<input type="password" name="txtpassword2" id="txtpassword2" class="info" />
</label>
</td>
</tr>
<td colspan="2" style="font-size:12px">**Passwords must be at least 8 characters in lenght.</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2">
<label>
<input type="radio" name="terms" value="terms" id="terms" />I agree to the Terms and Conditions</label>
</td>
</tr>
<br>
<br>
<br>
<tr>
<td colspan="2">
<label>
<input type="radio" name="notify" value="notify" id="notify" />I want to receive notifications by text/email about new products in stock.</label>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>
<label>
<input type="submit" name="Login" id="Login" value="Login" onclick="checkdetails()" />
</label>
</td>
<td>
<label>
<input type="reset" name="Reset" id="Reset" value="Reset" />
</label>
</td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
How can I achieve this? Any suggestions?
If you would like to save form data within a cookie (which is not recommended as stated in the comments), you will have to use JavaScript like this:
<script type="text/javascript">
var today = new Date();
var expiry = new Date(today.getTime() + 30 * 24 * 3600 * 1000); // plus 30 days
function setCookie(name, value)
{
document.cookie=name + "=" + escape(value) + "; path=/; expires=" + expiry.toGMTString();
}
function storeValues(form)
{
setCookie("field1", form.field1.value);// replace with your form fields
setCookie("field2", form.field2.value);
setCookie("field3", form.field3.value);
setCookie("field4", form.field4.value);
return true;
}
</script>
Also, you will need to call the setCookie function by calling it when the form's submit button is clicked, like this:
<input type="submit" onclick="storeValues(this.form)" value="Submit" />

Password validation for javascript

Hey I have a password validation function that I'm having trouble with. I have it working so that the user is immediately warned when the passwords don't match. However, I also want it to state that the password should have letters and numbers, and it can't be less than 6 characters. No matter what way I do it it won't work. Here is what I have that works so i just need help adding the rest. Thanks
<head>
function checkpass(pass1,pass2)
{
pass1=document.getElementById("txtpassword").value;
pass2=document.getElementById("txtpassword2").value;
if (pass1 != pass2)
{
alert ("Passwords must match");
}
}
</head>
<body>
<form action="Home.html" method="post" name="form1" onsubmit="Display()">
<table bgcolor="white" width="700" border="0" align="center">
<col width="200">
<col width="200">
<tr>
<th colspan = "2" align = "center" bgcolor="grey"> Fill the form to sign up. </th>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Forename</td>
<td>Surname</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtname" id="txtname" class="info" required>
<td><label>
<input type="text" name="txtsurname" id="txtsurname" class="info" required />
</label></td>
</tr>
<tr>
<td> Create Username</td>
<td> Birthday</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtusername" id="txtusername" class="info" required/>
</label></td>
<td><select name="DOBMonth" required>
<option value=""> Month </option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay" required>
<option value=""> Day </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>
<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="DOBYear" required>
<option value=""> Year </option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1998</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select>
<tr>
<td>Email</td>
<td>Confirm Email</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtemail" id="txtemail" class="info" onchange="ValidateEmail(txtemail)" required />
<td><label>
<input type="text" name="txtemail2" id="txtemail2" class="info" onchange="ValidateEmail2(txtemail2)" required/>
</label></td>
</tr>
<tr>
<td>Password</td>
<td>Confirm Pasword</td>
</tr>
<tr>
<td><label>
<input type="password" name="txtpassword" id="txtpassword" class="info" required/>
<td><label>
<input type="password" name="txtpassword2" id="txtpassword2" class="info" required/ onchange="checkpass(txtpassword,txtpassword2)">
</label></td>
</tr>
<td colspan="2" style="font-size:12px">**Passwords must be at least 8 characters in length.</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2">
<label>
<input type="checkbox" name="terms" value="terms" id="terms" required/>
I agree to the Terms and Conditions</label>
</td></tr>
<br><br><br>
<tr>
<td colspan="2"><label>
<input type="checkbox" name="notify" value="notify" id="notify" />
I want to receive notifications by text/email about new products in stock.</label>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><label>
<input type="submit" name="Login" id="Login" value="Login"/>
</label></td>
<td><label>
<input type="reset" name="Reset" id="Reset" value="Reset" />
</label></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
</body>
Here is the code
function checkpass() {
var pass1=document.getElementById("txtpassword").value;
var pass2=document.getElementById("txtpassword2").value;
if (pass1 !== pass2) {
alert ("Passwords must match");
} else if (pass1.length < 6) {
// too short
} else if (!/[0-9]/.test(pass1)) {
// no digit
} else if (!/[A-Za-z]/.test(pass1)) {
// no character
} else {
return true; // return true when password matched rules
}
return false; // false otherwise
}

Show details from 3 drop down menus

I have page on a website I'm doing for college and I've a problem with the registration page. I want a new window to open with the details I have entered. Below is the code I was given that shows the data that's entered into one drop down menu but the birthday one has 3 menus, one for day, month, year. I need help receiving the code from these 3 drop down menus rather than just one. Thanks for your help!
function getdropdownvalue()
{
var range = document.form1.DOBDay.selectedIndex
return document.form1.DOBDay.options[range].value
}
<body>
<form action="" method="post" name="form1" onsubmit="checkdetails()">
<table bgcolor="white" width="700" border="0" align="center">
<col width="200">
<col width="200">
<tr>
<th colspan = "2" align = "center" bgcolor="grey"> Fill the form to sign up. </th>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2" style="font-size:12px">Fields marked with * are mandatory</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td>Name*</td>
<td>Surname*</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtname" id="txtname" class="info" />
<td><label>
<input type="text" name="txtsurname" id="txtsurname" class="info" />
</label></td>
</tr>
<tr>
<td> Create Username*</td>
<td> Birthday</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtusername" id="txtusername" class="info"/>
</label></td>
<td><select name="DOBMonth">
<option> Month </option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay">
<option> Day </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>
<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="DOBYear">
<option> Year </option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1998</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select>
<tr>
<td>Email*</td>
<td>Confirm Email*</td>
</tr>
<tr>
<td><label>
<input type="text" name="txtemail" id="txtemail" class="info" onchange="ValidateEmail(txtemail)" />
<td><label>
<input type="text" name="txtemail2" id="txtemail2" class="info" onchange="ValidateEmail2(txtemail2)"/>
</label></td>
</tr>
<tr>
<td>Password*</td>
<td>Confirm Pasword*</td>
</tr>
<tr>
<td><label>
<input type="password" name="txtpassword" id="txtpassword" class="info"/>
<td><label>
<input type="password" name="txtpassword2" id="txtpassword2" class="info"/>
</label></td>
</tr>
<td colspan="2" style="font-size:12px">**Passwords must be at least 8 characters in lenght.</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td colspan="2">
<label>
<input type="radio" name="terms" value="terms" id="terms" />
I agree to the Terms and Conditions</label>
</td></tr>
<br><br><br>
<tr>
<td colspan="2"><label>
<input type="radio" name="notify" value="notify" id="notify" />
I want to receive notifications by text/email about new products in stock.</label>
</td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><label>
<input type="submit" name="Login" id="Login" value="Login" onclick="checkdetails()" />
</label></td>
<td><label>
<input type="reset" name="Reset" id="Reset" value="Reset" />
</label></td>
</tr>
<tr>
<td> </td>
</tr>
</table>
</form>
Just get the value of all the elements you need one by one (since you say you want month and day in numeric format I get the index of these elements not the value)
function getdropdownvalue()
{
var month = document.form1.DOBMonth.selectedIndex;
var day = document.form1.DOBDay.selectedIndex;
var year = document.form1.DOBYear.options[document.form1.DOBDay.selectedIndex].value;
alert(month+' '+day+' '+year);
return month+' '+day+' '+year;
}
Before I give you the solution for that, I noticed in your code that you are missing a few closing tags, filling the dropdowns in your code, use of table for formatting etc. These are few suggestions :
Fill the dropdowns using JS.
Use a good editor which helps you identify missed tags.
Add id to your select/input statements for easier working through JS.
You a framework like Bootstrap so that its easy to format your page and also avoid usage of <table> for formatting of the page.
That's a simple snippet for you:
function a() {
var m = document.getElementById("DOBMonth");
var y = document.getElementById("DOBYear");
var d = document.getElementById("DOBDay");
alert(" D:"+d.options[d.selectedIndex].value+" M:"+m.options[m.selectedIndex].value+" Y:"+y.options[m.selectedIndex].value);
}
<select name="DOBMonth" id="DOBMonth">
<option> Month </option>
<option value="January">January</option>
<option value="Febuary">Febuary</option>
<option value="March">March</option>
<option value="April">April</option>
<option value="May">May</option>
<option value="June">June</option>
<option value="July">July</option>
<option value="August">August</option>
<option value="September">September</option>
<option value="October">October</option>
<option value="November">November</option>
<option value="December">December</option>
</select>
<select name="DOBDay" id="DOBDay">
<option> Day </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>
<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="DOBYear" id="DOBYear">
<option> Year </option>
<option value="2016">2016</option>
<option value="2015">2015</option>
<option value="2014">2014</option>
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1998</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
<option value="1989">1989</option>
<option value="1988">1988</option>
<option value="1987">1987</option>
<option value="1986">1986</option>
<option value="1985">1985</option>
<option value="1984">1984</option>
<option value="1983">1983</option>
<option value="1982">1982</option>
<option value="1981">1981</option>
<option value="1980">1980</option>
<option value="1979">1979</option>
<option value="1978">1978</option>
<option value="1977">1977</option>
<option value="1976">1976</option>
<option value="1975">1975</option>
<option value="1974">1974</option>
<option value="1973">1973</option>
<option value="1972">1972</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
<option value="1968">1968</option>
<option value="1967">1967</option>
<option value="1966">1966</option>
<option value="1965">1965</option>
<option value="1964">1964</option>
<option value="1963">1963</option>
<option value="1962">1962</option>
<option value="1961">1961</option>
<option value="1960">1960</option>
<option value="1959">1959</option>
<option value="1958">1958</option>
<option value="1957">1957</option>
<option value="1956">1956</option>
<option value="1955">1955</option>
<option value="1954">1954</option>
<option value="1953">1953</option>
<option value="1952">1952</option>
<option value="1951">1951</option>
<option value="1950">1950</option>
<option value="1949">1949</option>
<option value="1948">1948</option>
<option value="1947">1947</option>
</select>
<button onclick="a()">Click</button>

Stop URL parameters being added on submit of form

Okay, so this may be a stupid question, but I'm attempting to write a simple JavaScript app-thing (please excuse my terminology, I'm new to this) which includes a form, except I'm doing it using only HTML and JS (and CSS), so I'm accessing the values in the form by using document.getElementById.
The thing is the Javascript function is supposed to display something, based on the form values (it's basically a GPA calculator), but the things to display simply flash on the screen, and then disappear when I run the HTML file as is, and don't show up at all when I run it through my actual domain. I feel like the reason for this is that after you submit the form, a bunch of URL parameters get tacked on, thus refreshing the HTML, and deleting the newly added innerHTML.
Here's the code for my form:
<form id="calculator" name="calculator">
<table>
<thead>
<tr>
<td>Class Number</td>
<td>Grade</td>
<td>AP/Non-AP</td>
<td>GPA</td>
</tr>
</thead>
<tbody>
<tr>
<td class="classnumber">Class 1</td>
<td>
<select id="class1GPA" name="class1GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class1AP" name="class1AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class1disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 2</td>
<td>
<select id="class2GPA" name="class2GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class2AP" name="class2AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class2disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 3</td>
<td>
<select id="class3GPA" name="class3GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class3AP" name="class3AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class3disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 4</td>
<td>
<select id="class4GPA" name="class4GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class4AP" name="class4AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class4disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 5</td>
<td>
<select id="class5GPA" name="class5GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class5AP" name="class5AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class5disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 6</td>
<td>
<select id="class6GPA" name="class6GPA">
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class6AP" name="class6AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class6disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 7</td>
<td>
<select id="class7GPA" name="class7GPA">
<option value="none">N/A</option>
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class7AP" name="class7AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class7disp">
</td>
</tr>
<tr>
<td class="classnumber">Class 8</td>
<td>
<select id="class8GPA" name="class8GPA">
<option value="none">N/A</option>
<option value=4.4 selected>A+</option>
<option value=4.0>A</option>
<option value=3.7>A-</option>
<option value=3.4>B+</option>
<option value=3.0>B</option>
<option value=2.7>B-</option>
<option value=2.4>C+</option>
<option value=2.0>C</option>
<option value=1.7>C-</option>
<option value=1.4>D+</option>
<option value=1.0>D</option>
<option value=0.7>D-</option>
<option value=0>F</option>
</select>
</td>
<td>
<select id="class8AP" name="class8AP">
<option value=False selected>Regular Class</option>
<option value=True>AP Class</option>
</select>
</td>
<td id="class8disp">
</td>
</tr>
</br>
</tbody>
</table>
<input onClick="showGPA()" id="submit" type="submit" value="Calculate!"/>
</form>
And the Javascript that is supposed to process it (it doesn't actually display an average yet, but that's less important than figuring out why it just flashes/doesn't display):
function showGPA() {
var grades = {};
grades.classesNumber = 6
if (document.getElementById('class1AP').value === "True") {
grades.class1 = parseFloat(document.getElementById('class1GPA').value) + 0.5;
} else {
grades.class1 = parseFloat(document.getElementById('class1GPA').value);
}
document.getElementById('class1disp').innerHTML = grades.class1.toFixed(1);
if (document.getElementById('class2AP').value === "True") {
grades.class2 = parseFloat(document.getElementById('class2GPA').value) + 0.5;
} else {
grades.class2 = parseFloat(document.getElementById('class2GPA').value);
}
document.getElementById('class2disp').innerHTML = grades.class2.toFixed(1);
if (document.getElementById('class3AP').value === "True") {
grades.class3 = parseFloat(document.getElementById('class3GPA').value) + 0.5;
} else {
grades.class3 = parseFloat(document.getElementById('class3GPA').value);
}
document.getElementById('class3disp').innerHTML = grades.class3.toFixed(1);
if (document.getElementById('class4AP').value === "True") {
grades.class4 = parseFloat(document.getElementById('class4GPA').value) + 0.5;
} else {
grades.class4 = parseFloat(document.getElementById('class4GPA').value);
}
document.getElementById('class4disp').innerHTML = grades.class4.toFixed(1);
if (document.getElementById('class5AP').value === "True") {
grades.class5 = parseFloat(document.getElementById('class5GPA').value) + 0.5;
} else {
grades.class5 = parseFloat(document.getElementById('class5GPA').value);
}
document.getElementById('class5disp').innerHTML = grades.class5.toFixed(1);
if (document.getElementById('class6AP').value === "True") {
grades.class6 = parseFloat(document.getElementById('class6GPA').value) + 0.5;
} else {
grades.class6 = parseFloat(document.getElementById('class6GPA').value);
}
document.getElementById('class6disp').innerHTML = grades.class6.toFixed(1);
if (document.getElementById('class7GPA').value !== "none") {
if (document.getElementById('class7AP').value === "True") {
grades.class7 = parseFloat(document.getElementById('class7GPA').value) + 0.5;
} else {
grades.class7 = parseFloat(document.getElementById('class7GPA').value);
}
document.getElementById('class7disp').innerHTML = grades.class7.toFixed(1);
grades.classesNumber++;
} else {
grades.class7 = 0;
}
if (document.getElementById('class8GPA').value !== "none") {
if (document.getElementById('class8AP').value === "True") {
grades.class8 = parseFloat(document.getElementById('class8GPA').value) + 0.5;
} else {
grades.class8 = parseFloat(document.getElementById('class8GPA').value);
}
document.getElementById('class8disp').innerHTML = grades.class8.toFixed(1);
grades.classesNumber++;
} else {
grades.class8 = 0;
}
}
How is it possible for me to stop 16 different URL parameters from being added on when the form submits?
Now my guess as to why it doesn't work may be completely wrong, as I said, I'm new to this. (While trying to make this work, I noticed that one of the problems was that I spelt .innerHTML as .innnerHTML)
But if someone could help me make this work, preferably without learning PHP or something, that would be great. Thanks!
When you submit a form, it will do an HTTP post to the action you specify. If you're looking to run some javascript on your form here, and stop it from posting, add 'return false' on your submit.
http://jsbin.com/icobam/2/edit
<form id="calculator" name="calculator" onsubmit="return false;">
<form id="calculator" name="calculator" onsubmit='showGPA();' method="post">
Inserting method="post" will stop the form from adding parameters in the URL.
Remove the onclick statement from the submit button:
<input onClick="showGPA()" id="submit"
to
<input id="submit"
Change the form to:
<form id="calculator" name="calculator" onsubmit='showGPA(); return false;'>
Note: if there are any javascript errors within the showGPA function then the form will be submitted and cause a page refresh. The only way to prevent this is to ensure that there are no javascript errors.

Categories