How to mix Javascript and PHP?

I want to call a Javascript function in PHP. This does not execute the function but shows the text on the screen:

	if ((strlen($chooseYear) < 1)) { $chooseYear = date('Y');
echo "messageDefaultYearInserted()";
	}

How do I execute a function in a case like this? In this case, the function will write a message in innerHTML in a span.

You can’t call a javascript function from php. PHP is processed on the server and passed to the browser as a complete rendered page. Javascript is executed by the browser after the page has already been returned to the browser.

Why not create ‘messageDefaultYearInserted()’ in your php script and call it that way?

I’m rusty with my PHP! I’ll try that next. Thanks for helping.

This did not work (text did not appear):

<?php
/* PHP FUNCTIONS */
function messageDefaultYearInserted(){
echo "document.getElementById('messages').innerHTML = '<p>Current year displayed.</p>'";
}
?>

<!-- warning messages displayed here -->
<div id="messages" style="color:red; clear:left;"></div>

<?php
[...php code...]
	if ((strlen($chooseYear) < 4)) { $chooseYear = date('Y');messageDefaultYearInserted();}
?>

Am I warm? :slight_smile:

This works (adding the <script> tags):

function messageNotWithinRange() {
echo “<script>document.getElementById(‘messages’).innerHTML = ‘<p>What you entered falls outside the range allowed in the Year field.</p>’</script>”;
}