0

I have a PHP form in which i use java script to get birth date value from a calendar, then i wrote a onchange java script to calculate age from the value of the textbox and display it in another textbox.. but onchange function is not working...

This is the textbox code to get the birth date from calendar from external javascript that i downloaded from some site.. this works fine.. it displays selected date from calendar in the textbox..

<input id="birthdate" name="birthdate" type="text" size="12" maxlength="10" readonly="readonly" onChange="UpdateAge();"  />
                                                <script language="JavaScript">
    new tcal ({
        // form name
        'formname': 'reg',
        // input name
        'controlname': 'birthdate'
    });

this is the function to calculate age..

 <script type="text/javascript">
    function UpdateAge(){
  var bdate = document.getElementById('birthdate').value;
  var cdate = date('d-m-Y');
  var info = (strtotime($cdate)-strtotime($bdate))/(24*60*60*365); 
  alert("hi");
  document.getElementById('age').value = info;
}
</script>

but nothing changes in the second textbox.. this is code of second textbox..

<input name="age" type="text" id="age" size="5" maxlength="2"  />

pls help me with a solution.. thanks in advance..

1
  • 6
    Your UpdateAge() function seem to contain PHP code. PHP codes runs on the server, javascript on the client. This won't work the way you think. Commented Jan 15, 2013 at 16:22

3 Answers 3

3
(strtotime($cdate)-strtotime($bdate))

This is not Javascript code. You have to find a library that can perform the same treatment than PHP's strtotime

Sign up to request clarification or add additional context in comments.

Comments

2

Function UpdateAge() has got PHP code

(strtotime($cdate)-strtotime($bdate))/(24*60*60*365); 

Which is invalid JS so I wouldn't work on client. PHP runs on server and JS runs on client.

Comments

1

strtotime is has said before strtotime is not a javascript function. It's a PHP function. Take look at Date object and its parse method : http://www.w3schools.com/jsref/jsref_parse.asp

1 Comment

Normally id say not to reference w3schools... but it looks like they are the only guys with docs on that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.