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
Share on other sites

Then all you need is....

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

This really has nothing to do with php.
Link to comment
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!?
:-\
Link to comment
Share on other sites


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