letournp Posted October 28, 2015 Share Posted October 28, 2015 I have a form where users select a starttime and endtimes (via a timepicker). I am calling a function using the onchange event to get the time diff between these 2 times, But for some reason the function is not triggered (weel sort off) the funny thing is that to test, I just added an allert to the function and that works great but then I add the remainder of the code the function does not trigger. Code that tiggeres corretly is this; <script> function thours() { alert("Function triggered"); } </script> and when I add the rest of my code to the same fucntion like this, it does not trigger. <script> function thours() { alert("Function triggered"); $date1 = <?php echo $_POST['TimeStart'] ?>; $date2 = <?php echo $_POST['TimeEnd'] ?>; if ($date1 != "") && ($date2 != "") { $diff = $date2->diff($date1); $hours = $diff->h; $hours = $hours + ($diff->days*24); echo $hours; document.getElementsById("Hours").value = $hours; } } </script> I dont get it. help thanks Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/ Share on other sites More sharing options...
ginerjm Posted October 28, 2015 Share Posted October 28, 2015 That's not how you mix in your vars to the JS function. You can't use php in a js function since one is client-side and the other is server-side. Make you js function retrieve the values you want from the dom by using simple js things such as getElementById. (google it). Ps - Is English not your normal language? Cause if it is, how are you ever able to spell php functions correctly? Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/#findComment-1524589 Share on other sites More sharing options...
letournp Posted October 28, 2015 Author Share Posted October 28, 2015 No english is not my normal language!. I am learning an doing the best I can. thanks Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/#findComment-1524592 Share on other sites More sharing options...
ginerjm Posted October 28, 2015 Share Posted October 28, 2015 In that case - forget my comment. As for your problem though - do you understand what I posted? Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/#findComment-1524596 Share on other sites More sharing options...
letournp Posted October 28, 2015 Author Share Posted October 28, 2015 yes I did. like I said I am a newby and did not know that i could not use php in a function, I also have never used functions before, learnt something new. thanks I have managed to get the values from the textboxes (used an alert to verify) that worked. now trying to calculate the time diff between time1 and time2 and return that to a hidden field so that I can insert into a table. Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/#findComment-1524598 Share on other sites More sharing options...
ginerjm Posted October 28, 2015 Share Posted October 28, 2015 Why do you need to do this with JS in real time? Simply have the uses submit the form with the entered times and let php do the work. Quote Link to comment https://forums.phpfreaks.com/topic/298890-function-mystery/#findComment-1524606 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.