Jump to content

Function mystery


letournp

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.