I have a bit of jQuery that allows people to add form fields dynamically based on the number of children they have.
var childCount = $('.newchildsub').length + 1;
$('.newchildsub:last').after($('.newchildsub:first').clone().removeAttr('id').attr('id', 'child-' + childCount).insertAfter('.newchildsub:last'));
$('#child-' + childCount).find('#childId').remove();
$('#child-' + childCount).find('#gender').attr('name', 'children[child-' + childCount + '][gender]').attr('value', '');
$('#child-' + childCount).find('#month').attr('name', 'children[child-' + childCount + '][month]').attr('value', '');
$('#child-' + childCount).find('#year').attr('name', 'children[child-' + childCount + '][year]').attr('value', '');
The code works prefect to add the fields and increase the name but when I click submit and print_r($_POST);, the dynamically created field is not there.
Below is an example of my html including the dynamically generated field (child-3), which is based of the first one as you can see in the jQuery.
<div id="child-1" class="newchildsub">
<h4>Gender: </h4><input type="hidden" id="childId" name="children[child-1][id]" value="394820">
<select name="children[child-1][gender]" id="gender">
<option value="">--</option>
<option value="male" selected>Male</option>
<option value="female">Female</option>
</select>
<h4>Birthday: </h4>
<select name="children[child-1][month]" id="month">
<option value="">Month</option>
<option value="01">January</option>
<option value="02" selected>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="children[child-1][year]" id="year">
<option value="">Year</option>
<option value="2012" selected>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>
</select><br><br>
</div>
<div id="child-2" class="newchildsub">
<h4>Gender: </h4>
<input type="hidden" id="childId" name="children[child-2][id]" value="394821">
<select name="children[child-2][gender]" id="gender">
<option value="">--</option>
<option value="male">Male</option>
<option value="female" selected>Female</option>
</select>
<h4>Birthday: </h4>
<select name="children[child-2][month]" id="month">
<option value="">Month</option>
<option value="01" selected>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="children[child-2][year]" id="year">
<option value="">Year</option>
<option value="2012">2012</option>
<option value="2011" selected>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>
</select><br><br>
</div>
<div id="child-3" class="newchildsub">
<h4>Gender: </h4>
<select id="gender" name="children[child-3][gender]">
<option value="">--</option>
<option selected="" value="male">Male</option>
<option value="female">Female</option>
</select>
<h4>Birthday: </h4>
<select id="month" name="children[child-3][month]">
<option value="">Month</option>
<option value="01">January</option>
<option selected="" 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 id="year" name="children[child-3][year]">
<option value="">Year</option>
<option selected="" 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>
</select><br><br>
</div>
According to your code you are changing the id, but not the name attribute for the cloned fields.
The name attribute has to be unique and that's why you don't see the dynamically added fields.
Add your name attribute into your jQuery code.
You have to manage name of your input element.
You can give them the name uniquely or give the name as un-indexed array like children[gender][]. Php will parse them and treat as array by creating index automatically for that. This link may help
Related
I have made a users account page where they can update their information.
For my first issue I will be using birthday as an example (it uses three dropdown bars which are saved as the variables $dobday $dobmonth and $dobyear) if a user only inputs one of the variable's - eg $dobdayand then they press 'update' the php will save that to the database, however this is then saving an incomplete date of birth, i have thought about adding a 'requirerd' function on the form, however i want it to be optional if the user only wants to fill out one piece of information. Is there any way I can resolve this by an if statement or another function that I am un aware of?
Secondly, if I enter a number in the phone number field and leave the rest of the fields blank on the form, when the database is updated, the other fields that were not filled in but had something saved in them on the database are wiped.
I am still learning web design so forgive me for my poor code. Thank you very much for your help and time :)
HTML form:
<form class="containerformAccounts" method="post" action="accountpageForm.php" >
<div class="container">
<h1>Edit your account</h1>
<hr>
<h3>Enter your telephone number</h3>
<label for="mobNumber"><b><i>Phone Number:</i></b></label>
<input type="mobNumber" maxlength="11" name="mobNumber" id="mobNumber" placeholder="Enter phone number"> <!--ommiting 'required' as this part is optional-->
</br>
<h3>Enter your address</h3>
<label for ="addressLine"><b><i>Address Line:</i></b></label>
<input type="addressLine" name="addressLine" id="addressLine" placeholder="Enter your address line">
<label for ="townCity"><b><i>Town/City:</i></b></label>
<input type="townCity" name="townCity" id="townCity" placeholder="Enter your town/city">
<label for ="postcode"><b><i>Postcode:</i></b></label>
<input type="postcode" name="postcode" id="postcode" placeholder="Enter your Postcode">
<label for ="country"><b><i>Country:</i></b></label>
<input type="country" name="country" id="country" placeholder="Enter your country">
</br>
<h3>Enter your birthdate</h3>
<label for ="dob-day"><b><i>Birthday:</i></b></label>
<select name="dobday" id="dobday">
<option value="Day">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="dobmonth" id="dobmonth">
<option value="Month">Month</option>
<option value="January">January</option>
<option value="Feburary">Feburary</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="dobyear" id="dobyear">
<option value="Year">Year</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>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</p>
<hr>
<h3>Change your password</h3>
</p><a href='passwordChange.php'>Change your password</a> </br>
</br>
<input type='submit' name='submit' value= 'Update'>
</hr>
</hr>
</div>
PHP:
<?php
session_start();//creating session
$user=$_SESSION['firstName'];
if (isset($_POST['submit'])){
$connectDB = mysqli_connect("localhost","root","") or die("cant connect"); //proving the database connection details and saving it as a variable
mysqli_select_db($connectDB, "registration"); //table name
// BIRTHDAY
$updateDBvalues=array(); //creating array to be used in query
$updateArray=array(); //creating array
$dobday=$_POST['dobday']; //importing variables
$dobmonth=$_POST['dobmonth'];//importing variables
$dobyear=$_POST['dobyear'];//importing variables
//ADDRESS
$addressLine=$_POST['addressLine'];//importing variables
$townCity=$_POST['townCity'];//importing variables
$postcode=$_POST['postcode'];//importing variables
$country=$_POST['country'];//importing variables
//NUMBER
$mobNumber=$_POST['mobNumber'];//importing variables
//NUMBER BELOW
if(!empty($mobNumber))
$updateArray[]="mobNumber='".$mobNumber."'";
//ADDRESS BELOW
if(!empty($addressLine))//if the variables is NOT empty
$updateArray[]="addressLine='".$addressLine."'";//add the variable to the array
if(!empty($townCity))//if the variables is NOT empty
$updateArray[]="townCity='".$townCity."'";//add the variable to the array
if(!empty($postcode))//if the variables is NOT empty
$updateArray[]="postcode='".$postcode."'";//add the variable to the array
if(!empty($country))//if the variables is NOT empty
$updateArray[]="country='".$country."'";//add the variable to the array
//BIRTHDAY BELOW
if(!empty($dobday))//if the variables is NOT empty
$updateArray[]="dobday='".$dobday."'";//add the variable to the array
if(!empty($dobmonth))//if the variables is NOT empty
$updateArray[]="dobmonth='".$dobmonth."'";//add the variable to the array
if(!empty($dobyear))//if the variables is NOT empty
$updateArray[]="dobyear='".$dobyear."'";//add the variable to the array
$updateDBvalues=$updateArray;
$updateDBvalues_imploded=implode(',',$updateDBvalues); // takes the array and makes one string
if(!empty($updateDBvalues)){ //if the array is NOT empty
$query="UPDATE users SET $updateDBvalues_imploded WHERE firstName='$user'";//sql query to save entries into table
$connQuery=mysqli_query($connectDB,$query);//querys database
die("Succesfully updated, return to <a href='accountPage.php'>Accounts page</a>");//successful query, provides link for user to return to account page
}else{
die ("Unable to save new infomation to the database, please try again. <a href='accountPage.php'>Accounts page</a>");//kills program, outputting that the query was unsuccessful, provides link for user to return to account page
}
}
?>
Well, you don't want to nullify your previously save data in fields. For that you can use COALESCE function in MySQL. It returns the first not null value in the parameters.
For example, you can set fields this way:
$updateArray[]="dobyear= coalesce('".$dobyear."', dobyear)"
But what about the situations where you really want to nullify the field. The better approach will be to populate the form with data in the database. Now, you can simply save the complete form with changes by the user.
So I have this code that creates a drop down box to select a month, another drop down box to select the day, then a text field to type in a year. My question is what to do next in php to be able to select a certain month, day, and year to where, when i click "calculate," it will display what day of that year it is?
So for example, if i choose January 4 2015, when i click calculate, i want it to display 4 since it is the 4th day of the calendar year. How do I do that?
<form action="calc.php" method="post">
Month: <select name="month">
<option value="january">January</option>
<option value="february">February</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>
Day: <select name="day">
<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>
<input name="entry" type="text" size="10" placeholder="Year">
<input type="submit" value="Calculate">
</form>
You can use date:
echo date("z", strtotime('2015-01-04'));
But it returns:
The day of the year (starting from 0)
So you need to add 1 to the result.
you can try the below code to get the day of the date . here i am using the same page for the post. if you are using another page for posting (i mean action page) use the php inside that file,
HTML
<form action="#" method="post">
Month: <select name="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>
Day: <select name="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>
<input name="year" type="text" size="10" placeholder="Year">
<input type="submit" name="submit" value="Calculate">
</form>
PHP
<?php
if(isset($_POST['submit']))
{
$date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];
echo date("l", strtotime($date));
}
?>
I am trying to make a dynamic form in CakePHP, but it seems not to work.
What can I do to make it work; I can't get data[AppliedCourses][0][institution], can't get data[AppliedCourses][1][institution], data[AppliedCourses][2][institution], etc.
<table id="mytable2" style="border: 0px;">
<tbody>
<tr id="InstitutionApplied0">
<td>
<button type="button" title="Click Here to remove Institution Applied" style="display: none;">-</button>
<div class="input text">
<label for="AppliedCourses0Institution">Institution Name</label>
<input name="data[AppliedCourses][0][institution]" type="text" id="AppliedCourses0Institution" />
</div>
<div class="input select">
<label for="AppliedCourses0CourseId">Course Of Interest</label>
<select name="data[AppliedCourses][0][course_id]" id="AppliedCourses0CourseId">
<option value="1">Applied Sciences</option>
<option value="2">Architecture & Built Enviroment</option>
<option value="3">Art & Design</option>
<option value="4">Business / Commerce</option>
<option value="5">Computing & IT</option>
<option value="6">Communication & Media Studies</option>
<option value="7">Education & Language</option>
<option value="8">Engineering</option>
<option value="9">Environmental Science</option>
<option value="10">Health Sciences</option>
<option value="11">Hospitality & Tourism</option>
<option value="12">Humanities & Social Sciences</option>
<option value="13">Law & Legal Studies</option>
<option value="14">Music</option>
<option value="15">Others</option>
</select>
</div>
<div class="input text">
<label for="AppliedCourse0Major">Major Field Of Course</label>
<input name="data[AppliedCourse][0][major]" maxlength="255" type="text" id="AppliedCourse0Major" />
</div>
<div class="input date">
<label for="AppliedCourses0IntakeMonth">Intake</label>
<select name="data[AppliedCourses][0][intake][month]" id="AppliedCourses0IntakeMonth">
<option value="01">January</option>
<option value="02">February</option>
<option value="03" selected="selected">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="data[AppliedCourses][0][intake][year]" id="AppliedCourses0IntakeYear">
<option value="2035">2035</option>
<option value="2034">2034</option>
<option value="2033">2033</option>
<option value="2032">2032</option>
<option value="2031">2031</option>
<option value="2030">2030</option>
<option value="2029">2029</option>
<option value="2028">2028</option>
<option value="2027">2027</option>
<option value="2026">2026</option>
<option value="2025">2025</option>
<option value="2024">2024</option>
<option value="2023">2023</option>
<option value="2022">2022</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015" selected="selected">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">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
</select>
</div>
<div class="input date">
<label for="AppliedCourses0SubmissionDateDay">Submission Date</label>
<select name="data[AppliedCourses][0][submission_date][day]" id="AppliedCourses0SubmissionDateDay">
<option value="01">1</option>
<option value="02" selected="selected">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="data[AppliedCourses][0][submission_date][month]" id="AppliedCourses0SubmissionDateMonth">
<option value="01">January</option>
<option value="02">February</option>
<option value="03" selected="selected">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="data[AppliedCourses][0][submission_date][year]" id="AppliedCourses0SubmissionDateYear">
<option value="2035">2035</option>
<option value="2034">2034</option>
<option value="2033">2033</option>
<option value="2032">2032</option>
<option value="2031">2031</option>
<option value="2030">2030</option>
<option value="2029">2029</option>
<option value="2028">2028</option>
<option value="2027">2027</option>
<option value="2026">2026</option>
<option value="2025">2025</option>
<option value="2024">2024</option>
<option value="2023">2023</option>
<option value="2022">2022</option>
<option value="2021">2021</option>
<option value="2020">2020</option>
<option value="2019">2019</option>
<option value="2018">2018</option>
<option value="2017">2017</option>
<option value="2016">2016</option>
<option value="2015" selected="selected">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">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
</select>
</div>
</td>
</tr>
<tr>
<td>
<button type="button" title="Click Here to add another Institution" onclick="addInstitution()">+</button>
</td>
</tr>
</tbody>
and the JS
<script>
var lastRowInstitutionApplied = 0;
function addInstitution() {
lastRowInstitutionApplied++;
$("#mytable2 tbody>tr:#InstitutionApplied0").clone(true).attr('id', 'InstitutionApplied' + lastRowInstitutionApplied).removeAttr('style').insertBefore("#mytable2 tbody>tr:#trAdd3");
$("#InstitutionApplied" + lastRowInstitutionApplied + " button").attr('onclick', 'removeInstitutionApplied(' + lastRowInstitutionApplied + ')').attr('style', 'display:block;');
$("#InstitutionApplied" + lastRowInstitutionApplied + " input:first").attr('name', 'data[appliedCourse][' + lastRowInstitutionApplied + '][institution]').attr('id', 'AppliedCourseInstitution' + lastRowInstitutionApplied);
$("#InstitutionApplied0" + lastRowInstitutionApplied + " select").attr('name', 'data[appliedCourse][' + lastRowInstitutionApplied + '][course_id]').attr('id', 'AppliedCourseCourseId' + lastRowInstitutionApplied);
$("#InstitutionApplied0" + lastRowInstitutionApplied + " input:eq(2)").attr('name', 'data[appliedCourse][' + lastRowInstitutionApplied + '][major]').attr('id', 'AppliedCourseCourseMajor' + lastRowInstitutionApplied);
$("#InstitutionApplied" + lastRowInstitutionApplied + " select(1)").attr('name', 'data[AppliedCourse][' + lastRowInstitutionApplied + '][intake]').attr('id', 'AppliedCourseIntake' + lastRowInstitutionApplied);
$("#InstitutionApplied" + lastRowInstitutionApplied + " select(2)").attr('name', 'data[AppliedCourse][' + lastRowInstitutionApplied + '][submission_date]').attr('id', 'AppliedCourseDate' + lastRowInstitutionApplied);
}
function removeInstitutionApplied(x) {
$("#InstitutionApplied" + x).remove();
lastRowInstitutionApplied--;
}
</script>
I got two php files the Add.php and Edit.php
In Add.php. A drop-down list of date and gender.
<tr>
<td width="400" id="caption" style="text-align:right;">Birthdate</td>
<td>
<select id="monthID" name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select id="dayID" name="day">
<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 id="yearID" name="year">
<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="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>
<option value="1993">1993</option>
<option value="1994">1994</option>
<option value="1995">1995</option>
<option value="1996">1996</option>
<option value="1997">1997</option>
<option value="1998">1998</option>
<option value="1999">1999</option>
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
<option value="2007">2007</option>
<option value="2008">2008</option>
<option value="2009">2009</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
<option value="2013">2013</option>
</select>
</td>
</tr>
<tr>
<td width="400" id="caption" style="text-align:right;">Gender</td>
<td id="caption" style="text-align:left;">
<select name="gender">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
</td>
</tr>
If I choose April 3 2010 and Female in the drop-downs. And insert it in my table_1
Assume this are drop-downs
Birthdate: April 3 2010
Gender: Female
Now, when I want to update the record(s) in Edit.php the drop-down value should be also the same
Birthdate: April 3 2010
Gender: Female
But instead it gives me this
Birthdate: January 1 2013
Gender: Male
It seems like a default value whenever I visit in Edit.php. (January 1 2013 Male - are the first items in the dropdowns)
How can I make [April 23 2013 Female] appear in my drop-down Instead of the first values that I didn't choose.
Thanks and sorry for the title I can't think for a good title for this.
Use an if statement to compare each value with your record and then add "selected" to this option
here is the basic concept hope you catch the idea
<?php
$array = array(1=>"January",
2=>"February",
3=>"March",
4=>"April",
5=>"May",
6=>"June",
7=>"July",
8=>"August",
9=>"September",
10=>"October",
11=>"November",
12=>"December");
$stored_data = "2";
foreach($array as $key => $val) {
if($stored_data == $key) {
echo "<option value='$key' selected='1'>$val</option>";
} else {
echo "<option value='$key'>$val</option>";
}
}
?>
Hi guys I'm using wamp server and I have a database with a date of birth field. I would simply like to know what data type would be suitable for the way date of birth is specified on my website (January 01 1996).
I would like to get a sample mysql query using the DATE_FORMAT() function. Most of the posts I've seen use SELECT and SELECT*. But I believe this should be replaced with UPDATE. As in UPDATE users SET dob = DATE_FORMAT(dob, '%m/%d/%Y')); .
Please find my code for how the date of birth is specified below:
<label id= "dob" for="dob">Date of birth:</label>
<!-- Month dropdown -->
<select name="dob" id="month" onchange="" size="1">
<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>
<!-- Day dropdown -->
<select name="dob" id="day" onchange="" size="1">
<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>
</select>
<select id="birthyear" name="dob">
<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>
<option value="1946">1946</option>
<option value="1945">1945</option>
<option value="1944">1944</option>
<option value="1943">1943</option>
<option value="1942">1942</option>
<option value="1941">1941</option>
<option value="1940">1940</option>
<option value="1939">1939</option>
<option value="1938">1938</option>
<option value="1937">1937</option>
<option value="1936">1936</option>
<option value="1935">1935</option>
<option value="1934">1934</option>
<option value="1933">1933</option>
<option value="1932">1932</option>
<option value="1931">1931</option>
<option value="1930">1930</option>
<option value="1929">1929</option>
<option value="1928">1928</option>
<option value="1927">1927</option>
<option value="1926">1926</option>
<option value="1925">1925</option>
<option value="1924">1924</option>
<option value="1923">1923</option>
<option value="1922">1922</option>
<option value="1921">1921</option>
<option value="1920">1920</option>
</select>
</p>
When saving date in the database, use the correct datatype DATE or DATETIME. Don't use VARCHAR
Pros:
easy to manipulate
requires no casting when manipulating data
index will be used (if a key was defined)
Cons
can't think of something..
If the reason why you want to save date in this format January 01 1996 is because it is more readable than 1996-01-01, then don't. This can be achieve using DATE_FORMAT() during the projection of the data.
$query = "INSERT INTO users (username, date_of_birth) ";
$query .= "VALUES ('John Doe', 'DATE: Manual Date', '2008-7-04')";
The manual date acts somewhat like most StringToTime functions in most scripting languages.
Another way to store time, and is actually a little bit more efficient, is to use the scripting language (like PHP) to format the time into microtome and store it in the database as an INT(11). So when retrieving the microtime back from the database you can just display it any way you like and is even faster to just do calculations with it :).