Forum Moderators: coopster

Message Too Old, No Replies

Javascript in PHP fuction

Problem passing parameters

         

jdjack

1:44 am on Jul 6, 2005 (gmt 0)

10+ Year Member



Another newbie here!.

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.

JAB Creations

1:49 am on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First off...
<script type="text/javascript">

Secondly are you passing just an alpha-numeric value? Best to ask in the PHP forums I suppose.

jatar_k

1:49 am on Jul 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld jdjack,

my guess would be that $message isn't being set properly

what does your assignment statement and function call look like?

jdjack

2:27 am on Jul 6, 2005 (gmt 0)

10+ Year Member



This is the message I'm trying to use. Does the '\n' in it cause a problem.

$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.

JAB Creations

6:45 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm no php expert but I think you have to set variables in single quotes though I could certainly be wrong.

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;?>

jatar_k

7:03 pm on Jul 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



have you tried just echo ing the variable inside the function?

function displayConfirm($message, $date){
echo 'this is it: ',$message;

and when you view source of the page using the proper var do you see anything weird? or is it just not there something like

var mess = "" ;

jatar_k

3:15 am on Jul 7, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



bump after move ;)