• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3184
  • Last Modified:

Include JS File Into PHP

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
MunchIt
Asked:
MunchIt
  • 6
  • 4
  • 2
1 Solution
 
netmunkyCommented:
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
 
Dennis MaederCommented:
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
 
MunchItAuthor Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
Dennis MaederCommented:
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
 
netmunkyCommented:
what is the <script> supposed to show? javascript is script, not html.
0
 
MunchItAuthor Commented:
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
 
Dennis MaederCommented:
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
 
MunchItAuthor Commented:
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
 
Dennis MaederCommented:
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
 
MunchItAuthor Commented:
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
 
Dennis MaederCommented:
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
 
Dennis MaederCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now