Jump to content

[SOLVED] php wont work in external js file???


darksniperx
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

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.

 Share

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