Jump to content

Calling javascript function from php


sazzie

Recommended Posts

My course.php script has no html tags but it does declare and internal javascript function such as follows :

[code]
            //include javascript to create calenders two instances needed for both coloured forms
$javascript = "<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"CalendarPopup.js\" type=\"text/javascript\"></SCRIPT>
<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
var cal = new CalendarPopup();
</SCRIPT>";

<form name="form1" action="" method="post">
  <input name="testName" value=""/>
<a href="#" onclick="cal.test(<?php print $g;?>);">Click for calendar</a>
</form>

[/code]

As you can see, the form attempts to call a function of CalendarPopup but IE and Firefox return a "Cal is Undefined" message. ???

Please help. I know 100% that the Calendar Popup works beautifully so it is not the problem. Any suggestions?  ;)
Link to comment
https://forums.phpfreaks.com/topic/34563-calling-javascript-function-from-php/
Share on other sites

I think I found the problem.

The $javascript object is not being created with the indicated text.

[code]
$javascript = "<SCRIPT LANGUAGE=\"JavaScript\" SRC=\"CalendarPopup.js\" type=\"text/javascript\"></SCRIPT>
<SCRIPT LANGUAGE=\"JavaScript\" type=\"text/javascript\">
var cal = new CalendarPopup();
var caltwo = new CalendarPopup();
</SCRIPT>";
[/code]

On echoing the contents of $javascript, I found there is nothing in there.

Can anyone see what might be wrong!?
:-\

Well a tip would be to use single quotes for your literal string so you don't have to escape all the double quotes.

Actually I don't see any conditional processing here, so you should just include it outside of your php tags
[code]
<?php
$g = 'whatever';
?>
<html>
<head>
<script language='javascript'>
var cal = new CalendarPopup();
var caltwo = new CalendarPopup();
</script>
</head>
<body>...[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.