Forum Moderators: coopster
I'm having a problem passing a parameter from a PHP function into Javascript within the function, as below:-
function displayConfirm($message, $date){
?>
<script LANGUAGE="JavaScript">
var mess = "<? echo $message;?>" ;
var x = confirm(mess);
if (x) {
window.location = "displayroster.php?startdate=<? echo $date?> ";
}else{}
</script>
<?php
}
It passes the date into the URL no problem, but does not seem to be able to read the variable '$message'. If I change variable 'mess' to a string it works okay, but I want to bring meesage in as a variable.
Any suggestions.
$rosterMessage = "A roster for the date entered has already been created. \n Press 'ok' to view this roster."
This is where I call the function:-
if (!empty($rosterMessage)) {
displayConfirm($rosterMessage, $inputDate);
}
If I don't pass the the message in as a variable, just type it in instead of 'mess', the confirm box displays okay and everything works. If I pass it as variable the confirm box doesn't display.
You could print a variable that already contains the Javascript in the first place however. This would be the serverside way of approaching it.
If you do so be suer to escape single quotes in javascript like this...
<?php $js = 'document.writeln(\'javascript writting html');?><?php print $js;?>