Dan Dan - 1 month ago 12
jQuery Question

merge values of form select

I would like to know if there is a way, to summarize three distinct values into one. Let me try to explain this better. I have a sign up page (PHP/SQL) with a date-picker:

<input type='text' name="dob" id="dob" value="" class="form-control" />


Which writes into a database using the table DOB where the birthday is stored (mm/dd/year)

However due to layout purposes and usability on this secific website I would much rather have this changed to three individual select items (month year date).

<select id="month" name="month">
<option>..</option>
</select>

<select id="day" name="day">
<option>..</option>
</select>

<select id="year" name="year">
<option>..</option>
</select>


Is there a way to have these three individual values combined so that they will still write in the dob dabtabase table without the need of creating separate ones?

Some expert help would be greatly appreciated

Answer

Frontend solution :

You can concat the values in a input and use this input on backend.

Backend solution :

You can create a variable with the three values like front-end solution and use this variable for store in DB.

Example front-end

function updateBirth()
{
  $("#dob").val($("#month").val()+"/"+$("#day").val()+"/"+$("#year").val())
}

$(".selectbirth").change(function(){
  updateBirth();
});


updateBirth();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type='text' name="dob" id="dob" value="" readonly="true" class="form-control" />

<select class='selectbirth' id="month" name="month">
  <option>1</option>
  <option>3</option>
  <option>4</option>
</select>

<select class='selectbirth' id="day" name="day">
  <option>1</option>
  <option>3</option>
  <option>4</option>
</select>

<select class='selectbirth' id="year" name="year">
  <option>2000</option>
  <option>2001</option>
  <option>2002</option>
</select>

Comments