?
Solved

Include JS File Into PHP

Posted on 2006-11-16
15
Medium Priority
?
3,165 Views
Last Modified: 2013-11-18
Hi there im wanting to include a JS file into php however when using the <?phpo include> i always seem to get a Unexpecred < at line so so, can some one assist me in this please
0
Comment
Question by:MunchIt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 2
15 Comments
 
LVL 8

Expert Comment

by:netmunky
ID: 17957522
include() is for including other php files.
the reason you are getting that error is it is trying to execute the javascript as php.

do you want the script available in the html?
try adding <script type="text/javascript" src="yourfile.js"></script> to the html section of you php file.
0
 
LVL 10

Expert Comment

by:dennis_maeder
ID: 17957553
MunchIt,
Don't use a PHP include for it.
Simply add a script block
<script type='text/javascript' language='javascript' SRC='/myscript.js'></script>
or even
<?php echo "<script type='text/javascript' language='javascript' SRC='/myscript.js'></script>" ?>
at the appropriate place, with myscript.js pointing to your js.
D
0
 

Author Comment

by:MunchIt
ID: 17964503
Thanks for the replies although this didnt work for me using either method i either get an error with the php block unexpected < at line bla bla and the <script type does not sow anything...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:dennis_maeder
ID: 17968061
Munchit,
How is your document strucured? php tags should not be nested.
Also quotation style " " and ' ' can be problematic if they become nested.
If you show your code I may be able to spot the error more easily.
D
0
 
LVL 8

Expert Comment

by:netmunky
ID: 17968069
what is the <script> supposed to show? javascript is script, not html.
0
 

Author Comment

by:MunchIt
ID: 17971948
The certain php file already contains a nav bar what im trying to install is a new nav bar the code seems to be written in php to remove the current bar i inserted // before the code to stop it from excecuting however when i place the new bar this is when it causes trouble please see original code below.



//-----------------------------------
//---- Menu System v0.1
//-----------------------------------
function menu ($selected = "home") {
      global $BASEURL;
      $script_name = $_SERVER["SCRIPT_FILENAME"];
      if (preg_match("/index/i", $script_name)) {
            $selected = "home";
      }elseif (preg_match("/browse/i", $script_name)) {
            $selected = "browse";
      }elseif (preg_match("/search/i", $script_name)) {
            $selected = "search";
      }elseif (preg_match("/upload/i", $script_name)) {
            $selected = "upload";
      }elseif (preg_match("/usercp/i", $script_name)) {
            $selected = "usercp";
      }elseif (preg_match("/forums/i", $script_name)) {
            $selected = "forums";
      }elseif (preg_match("/topten/i", $script_name)) {
            $selected = "topten";
      }elseif (preg_match("/rules/i", $script_name)) {
            $selected = "rules";
      }elseif (preg_match("/faq/i", $script_name)) {
            $selected = "faq";
      }elseif (preg_match("/links/i", $script_name)) {
            $selected = "links";
      }elseif (preg_match("/staff/i", $script_name)) {
            $selected = "staff";
      }else
            $selected = "";
      print ("<div class=\"shadetabs\"><ul>");
      print ("<li" . ($selected == "home" ? " class=selected" : "") . "><a href=\"index.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='User CP Home'; return true;\">Home</a></li>");
      print ("<li" . ($selected == "browse" ? " class=selected" : "") . "><a href=\"browse.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Browse Torrents'; return true;\">Browse</a></li>");
      print ("<li" . ($selected == "search" ? " class=selected" : "") . "><a href=\"search.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Search Torrents'; return true;\">Search</a></li>");
      print ("<li" . ($selected == "upload" ? " class=selected" : "") . "><a href=\"upload.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Upload Torrents'; return true;\">Upload</a></li>");
      print ("<li" . ($selected == "usercp" ? " class=selected" : "") . "><a href=\"usercp.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='User CP'; return true;\">User Control Panel</a></li>");
      print ("<li" . ($selected == "forums" ? " class=selected" : "") . "><a href=\"forums.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Forums'; return true;\">Forums</a></li>");
      print ("<li" . ($selected == "topten" ? " class=selected" : "") . "><a href=\"topten.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='TOP 10'; return true;\">Top 10</a></li>");
      print ("<li" . ($selected == "rules" ? " class=selected" : "") . "><a href=\"rules.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='rules'; return true;\">Rules</a></li>");
      print ("<li" . ($selected == "faq" ? " class=selected" : "") . "><a href=\"faq.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='FAQ'; return true;\">FAQ</a></li>");
      print ("<li" . ($selected == "links" ? " class=selected" : "") . "><a href=\"links.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Links'; return true;\">Links</a></li>");
      print ("<li" . ($selected == "staff" ? " class=selected" : "") . "><a href=\"staff.php\" onMouseout=\"window.status=''; return true;\" onMouseOver=\"window.status='Staff'; return true;\">Staff</a></li>");
}
//-----------------------------------
//---- Menu System v0.1
//-----------------------------------

now ehere the code shows a print this is a menu button from which i dont want i want to replace with my own JS file that i have made into a menu
0
 
LVL 10

Expert Comment

by:dennis_maeder
ID: 17972047
Simply add this line
print ( "<script type='text/javascript' language='javascript' SRC='myscript.js'></script>" );
before or after your menu system block where myscript.js is a refernce to your javascript.
D
0
 

Author Comment

by:MunchIt
ID: 17972560
ok dennis tried that and got this

Parse error: parse error, unexpected T_STRING in /var/www/vhosts/httpdocs/include/functions.php on line 824

also when inserting that bit of code the code under it all changes color this is what i need to insert in order to get my new nav menu up its a dhtml menu however its been made to a JS FILE

this code needs to go in the HEAD section of a HTML page

<script type="text/javascript" language="JavaScript1.2" src="stmenu.js"></script>

and this bit is the global section which is the menu itself

<script type="text/javascript" language="JavaScript1.2" src="menu.js"></script>

always seem to get an error regardless of what happens
0
 
LVL 10

Accepted Solution

by:
dennis_maeder earned 500 total points
ID: 17972762
PHP files consist of php tags <?php php stuff  ?> interspersed with HTML.
i.e. you can think of the file as HTML with an arbitrary amount of interpolated php which generates further html on the server end. PHP does this with echo or print functions, the arguments of which are quoted strings or the results of functions which generate text.

I believe your problem is that you are quoting something containing quotes of the same style:
e.g.
echo "
 "some stuff"
";
this actually looks like
echo "" some stuff "";
which is wrong and confuses the parser.
One technique to get around this is to alternate quote styles:
e.g.
echo "
 'some stuff'
";
this actually looks like
echo " 'some stuff' ";
which is OK.

To avoid complications it is better to break the file down into a series of pure php functions in php blocks and follow this with straight html with interpolated php.
e.g.
<?php
function showmenu(){
 php stuff - build html menu string
 print($menu)
}
?>

<?php
function showform(){
 different php stuff
 print($form);
}
?>

<head>
<script type="text/javascript" language="JavaScript1.2" src="stmenu.js"></script>
</head>

<body>
<script type="text/javascript" language="JavaScript1.2" src="menu.js"></script>

<? php showform() ?>
more html stuff...
</body>



0
 

Author Comment

by:MunchIt
ID: 17997817
dennis i dont know how to use php i can get by but as for function i dont have a single clue.

i have tried to place the menu in a place where i though it would work i dontget an error but then again i dont get the form either is there anyway you could help me further in this....could you possbily write the code i would need to place in php blocks and then give little instructon on how to place it on that page.


Thanks for the help
0
 
LVL 10

Expert Comment

by:dennis_maeder
ID: 18060175
In order to write code I need to see all code and all files.
This may be quite complex, and is possibly beyond the scope of EE and a web interface.
Is there any way you can make these files available?
D
0
 
LVL 10

Expert Comment

by:dennis_maeder
ID: 18192019
Questioner asked a question about including js in php. That question was answered but the request was escalated to a full code write, which required resources questioner was not able to provide.
D
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question