Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Move the javascript up top and make it a function so you can reference it</p> <p>Then add the onChange() to trigger the function when you change your select elements</p> <p>finally have the function output your calculation to somewhere on the page</p> <pre><code>&lt;?php $month = date("m"); //without leading zero(o) $year = date("Y"); //four digit format $day = date("d"); $st_year = "1950"; //Starting Year $month_names = array("January", "February", "March","April", "May", "June", "July", "August", "September", "October", "November", "December"); $Convertdays = $month."/".$day."/".$year; ?&gt; &lt;script type="text/javascript"&gt; function update() { var e = document.getElementById("month"); var month = e.options[e.selectedIndex].value; e = document.getElementById("day"); var day = e.options[e.selectedIndex].value; e = document.getElementById("year"); var year = e.options[e.selectedIndex].value; var birthDate = month + "/" + day + "/" + year var birth = new Date(year,month,day,0,0,0); var check = new Date(); var milliDay = 1000 * 60 * 60 * 24; // a day in milliseconds; var ageInDays = (check - birth) / milliDay; var ageInYears = Math.floor(ageInDays / 365 ); var age = parseInt(ageInDays / 365) ; if (age &lt; 3) { document.getElementById('Calculated').innerHTML = "You are under 3 years old, why are you on the computer?"; } else { document.getElementById('Calculated').innerHTML = "You are " + age; } } &lt;/script&gt; Enter your birth Date: &lt;div style="height:15px;"&gt;&lt;/div&gt; &lt;form name="Month_Year" id="Month_Year" method="post"&gt; &lt;select name="month" id="month" style="margin-right: 30px;" onChange="update();"&gt; &lt;?php for ($i=1; $i&lt;=12; $i++) { echo "&lt;option "; if ($i == $month) { echo "selected=\"selected\" "; } echo "value=\"$i\"&gt;", $month_names[$i-1], "&lt;/option&gt;\n"; } ?&gt; &lt;/select&gt; &lt;select name="day" id="day" style="margin-right: 30px;" onChange="update();"&gt; &lt;?php for ($i=1; $i&lt;=31; $i++) { echo "&lt;option "; if ($i == $day) { echo "selected=\"selected\" "; } echo "value=\"$i\"&gt;$i&lt;/option&gt;\n"; } ?&gt; &lt;/select&gt; &lt;select name="year" id="year" onChange="update();"&gt; &lt;?php for ($i=$st_year; $i&lt;=$year; $i++) { echo "&lt;option "; if ($i == $year) { echo "selected=\"selected\" "; } echo "value=\"$i\"&gt;$i&lt;/option&gt;\n"; } ?&gt; &lt;/select&gt; &lt;br&gt; &lt;/form&gt; &lt;div id="Calculated"&gt; calculated age goes here&lt;br&gt; &lt;/div&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload