darksniperx Posted October 21, 2007 Share Posted October 21, 2007 the following code is inside external js file. function showYear() { document.getElementById("pub_year_id").value = '<?php print date('Y'); ?>'; } the following code is inside html file ... <script type='text/javascript' src='scripts/entry_js_script.js'></script> ... <body onload="showYear();"> ... I get blank value, if I was to put the whole code inside html file it would work, but as soon as I put inside external js file, it just does not want to work. The following code I have used as an example, and I cant use pure js, since alot of my js script is mixed with php and it just got too big and hard to maintain when it is inside html file, so I put it into an external file. Am I doing something wrong or there is another way to get php and js to work inside an external file. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/ Share on other sites More sharing options...
Northern Flame Posted October 21, 2007 Share Posted October 21, 2007 Im not 100% sure, but I dont think you can use PHP in a file with the extention ".js" therefor it wont work in a Javascript file. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374501 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 anyway to confirm it, I can try renaming the file, I dont think that will do much of a difference. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374505 Share on other sites More sharing options...
Wes1890 Posted October 21, 2007 Share Posted October 21, 2007 try renaming the JS file to entry_js_script.php then, just make PHP echo the javascript, including the variable where needed Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374507 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 and how do I call it from my html file. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374511 Share on other sites More sharing options...
Wes1890 Posted October 21, 2007 Share Posted October 21, 2007 you need to call it from a php file using include() go to php.net and READ Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374513 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 ok, thx, I will check it out tomorrow morning, its 11:30 pm for me, but thx. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374516 Share on other sites More sharing options...
igor berger Posted October 21, 2007 Share Posted October 21, 2007 Why do you want to use php in the first place? There is a date function in JavaScript. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374517 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 I am creating mini feed, for computer science department withing my school as a project, I use php+mysql to to store and retirieve data and atom syndicate to create xml feeds. Which means that most of my js is to make things look more dynamic, and more or less pass data from php to js so that I could use it the data I have retrived from db later on any times I wanted. My php also generates a unique id based on urn:uuid scheme. Question, can I load data from external files without using include. Because if I will have 50 includes or something like that, but I will use 2 or 3 at the time, they will be different based on the form which is going to be loaded. I will post my file structure, and you might give me suggestion on how can I improve it. thx Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374750 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 ho here is the structure that I have been using. index.php page: <?php include ("fusion.php"); include ("forms.php"); $webForm = "Entry"; //specify which form you want to work with $form = new forms; // create an instance of selected class $form->setFormData($webForm); // populate class variables selected form ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" > <head> <meta http-equiv="Content-Script-Type" content="text/javascript; charset=UTF-8;" /> <title>Index Page!</title> <?php $form->outputHeaderData(); // echo any content in the head section ?> </head> <body <?php $form->ouputBodyAttributeData(); // echo any content that is suppose to be in body tag ?>> <?php $form->outputForm($webForm); // echo any content in the body section ?> </body> </html> forms.php contains functions that echo html data ex:"outputForm($webForm); " , but since the page started to get too big 1.5 k of lines an growing, I started to separate it, so the first thing I did was take out all js scripts and put forms.php into separate files. Which gave me the following error of php no working in js. So as the person said earlier to one of my post, put it into a separate php file which echoes the content. I though then, thats a good idea, I will make a separate php page for every form content and make form content call them and out put them where user wants to. But then if I use include on every single php file and not use them, i will just waste memory. Is there any way around it. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374812 Share on other sites More sharing options...
igor berger Posted October 21, 2007 Share Posted October 21, 2007 Okay, so put your JavaScript in php file and use include to get it. Put this in a file called myjs.php and get it with include. <javasritp> some JS </javascript> You may need to use <?php echo <<<END <javasritp> some JS </javascript END; ?> But I think it will work as a first example also. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-374821 Share on other sites More sharing options...
darksniperx Posted October 21, 2007 Author Share Posted October 21, 2007 echo <<<END END: I dont think it is supported in php4, anyways I got around it by using \n, works good for me. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-375053 Share on other sites More sharing options...
igor berger Posted October 21, 2007 Share Posted October 21, 2007 echo <<<END END: Is supported in PHP4, I use it in all my scripts. Just make sure there are no blank spaces at the end of each syntax command or you will get an eror. An when I say make sure, I mean put your mouse over at the end of END and slide it to the right, this are blind blank spaces, you cannot see them but they may be there. So when I copy paste echo <<<END I hit return at the end of END to make sure there is only a carage return charcter there. But you welcome to do as you like in terms of how you code display your PHP, just it makes it hard to debug when you use short hand... Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-375098 Share on other sites More sharing options...
darksniperx Posted October 22, 2007 Author Share Posted October 22, 2007 k, i'll try again Edit: I have removed the spaces before END; and it worked. Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-375127 Share on other sites More sharing options...
igor berger Posted October 22, 2007 Share Posted October 22, 2007 Live and learn! Will make your life much easier in debuging! Try no to use short hand... Quote Link to comment https://forums.phpfreaks.com/topic/74155-solved-php-wont-work-in-external-js-file/#findComment-375379 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.