I have a menu that appears on all my pages. It contains php for passing filenames to url's as follows. Here is a snippet...
document.write('<li>abc</li>');
document.write('<li>def</li>');
document.write('<li>ghi</li>');
It includes css and that works fine.
I have saved the menu as a seperate .js file and used...
<script src="js/menucss.js"></script>
...to load it from each page that I want it to appear. The menu displays and the css works fine - however the php does not parse and the page shows the php markup. What am I doing wrong?
Thanks in advance.
Neil
You shouldn't do it like that.
Have a page called menu.php saved somewhere, which includes:
<ul>
<li>abc</li>
<li>def</li>
<li>ghi</li>
</ul>
Then include it, on the server side (index.php):
<html>
<head>
<title></title>
</head>
<body>
<header>
<?php include("main.php"); ?>
</header>
</body>
</html>
This approach is far superior:
It does not require an extra request to get the data.
There will be no flickering or awkward moments of data loading, the menu would appear as if you've coded it directly into the page.
It's much faster.
It doesn't use JavaScript, which the client can choose to disable.
You confuse server side and client side. PHP is about server side and PHP code it is interpreted on server. JavaScript is about client side, and JavaScript runs when user already has gotten html page, so when you dinamically add php in html on client side by JavaScript it cannot be run.
From what I understood I think that you are Trying to run PHP inside a .js file.
You should include that code in a PHP file.
The problem you're experiencing is probably due to the fact that you're attempting to run PHP on a .js file, and your server is not configured to it.
Change the file's extension to .php and use:
<script src="js/menucss.php">
Related
I have HTML and PHP files that include the "header" HTML for my website. In HTML files I include this header using
<!--#include virtual="/top.ssi" -->.
top.ssi in turn includes other files:
<!--#include virtual="/navbar.ssi" -->
<!--#include virtual="/advertising/slider_advertising.ssi" -->
and slider_advertising.ssi includes:
<!--#include virtual="/advertising/advertising.php" -->
This is the critical file as it prepares advertising data for display.
All the above works great when run from HTML files.
Now I have some PHP-driven webpages (Logon.php) that also want to display the header of the website using top.ssi. This code starts with:
<!DOCTYPE HTML>
<html>
<head><title>Login to HVmusic</title>
<?php
virtual("/top.ssi");
...
Here is where the problem comes in. The virtual("/top.ssi"); executes OK, but stops executing after it encounters the PHP file that is included in top.ssi <!--#include virtual="/advertising/advertising.php" -->. The output from advertising.php is displayed and then the PHP output from logon.php stops. So the output stops without even displaying the entire header.
If I remove the statement for <!--#include virtual="/advertising/advertising.php" --> then the logon.php displays it's page normally (without, or course, that bit of the header displayed by advertising.php). So this tells me that PHP is having a problem with a virtual() file that includes another PHP file.
Is there any way to fix this? Is it some known restriction in PHP? I've been googling for two days and can find no mention of this issue. Thanks for any help you can offer.
Thanks for the tips, that clarified my thinking. I have just converted all my web pages to PHP and this solved the issue. Most of my pages were already PHP, so it wasn't much work to convert the HTML pages. All the include files I used were easily converted to use <? php require "filename"; ?> instead of <!--#include virtual="filename" -->
The underlying problem probably has to do with some conflict about running in PHP, then calling virtual() which calls an apache instance, which then calls another PHP instance to process the PHP include files I used.
Ok so I am tasked to work with this third party vendor that shows movie times on their end. We have the chance to co-brand our site on their end, so in order to do that we need to create a wrapper of our site so that the movie times can show inside the wrapper so it still looks like your on our site. One option is to have a header and footer included via js external file, that way we can make changes on our side and the updates will show on their side, so we dont have to send them a new header/footer every time we make updates to the wrapper. They suggested we do it this way:
<script src="http://oursite.com/header.php"></script>
<script src="http://oursite.com/footer.php"></script>
So I have raw html in header.php hosted on our side and then I added this in the header before anything else:
<?php header("Content-type: text/javascript"); ?>
However when the page loads I get this error:
SyntaxError: syntax error
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" xml
Im assuming there is a problem having raw html code in a js file? If so is there anyway around this?
You should add a link to proper javascript that push desired html to the page.
<script src="http://oursite.com/[somejsfile].js"></script>
You can get help from this answer on how to insert html elements using javascript
If you are going to use javascript to call an external file you need to use ajax. The problem then lies that you are taking about your site and that cannot be done when your domains do not match.
If you do it in the manner you have included you must declare the html in javascript variables then, on load push the data to where it needs to go.
var html = '<html> YOUR CODE WATCH OUT FOR SINGLE QUOTES </html>';
//check for load with jquery
$(document).ready(function(){
$('#header").html(html);
})
I'm learning all this web programming stuff after years writing .EXE Windows programs so bear with me.
I developed a basic .php and mysql website that works fine.
But I went to add javascript code to my index.php and I don't think the javascript code is executing.
My index.php has the form:
<?php
require_once blah blah
call_my_php_functionBlah();
?>
Then I added this code inside the php blocks of the '<\?\php' and "\?>" as follows:
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
Nothing showed up.
So I thought "ah-HAH, I blew it, all I need to do is -- move the javascript code outside
of the php block, at the bottom of index.php, and surely I'm good to go."
And still, Nothing showed up.
I checked the source of my 'known-good' javascript code and it said 'embed this javascript code
in your HTML file' so I thought "wow, I guess I need an index.html or something here."
So my questions:
1) should my index.php be able to run the javascript block of code?
I'm guessing 'No because index.php executes on the server and javascript runs on the client machine.'
2) How should I architect this if I want to keep my index.php, whose code works fine and I don't want to mess with it?
I'm thinking that this is an extremely basic client/server, php and javascript script organization issue that every web programmer knows how to handle, but like I said, I'm new to all this. I read in the archives about .htaccess etc. etc. but I
bet there's an easier way, and I'm not sure if the stuff I read applies.
the file name extension is completely irrelevant
PHP executes on the server and doesn't care at all about any Javascript
code inside <?php ?> tags must of course be valid PHP code to be executed by PHP
your browser receives whatever the result of your PHP execution is
you can use PHP code to output Javascript or simply have Javascript on the same page outside of <?php ?> tags
only whatever the browser receives matters, so use View Source
look at the browser's Javascript Console to debug client-side Javascript problems
Then I added this code inside the php blocks of the '" as follows:
Dont add your script inside the php block bring it outside php block.
After you are done with script you can reopen php block and write php again
index.php can run javascript, just that You need to echo the javascript code to put it in the page.
Anything that appears inside your php open/close tags has to be echoed or printed to be rendered to the html page. Anything outside your php open/close tags should appear in your html page but whether it works correctly or not is another matter not necessarily related to your php. The php interpreter doesn't run your javascript code, however, so it can't just sit inside your php tags.
Javascript will run inside .php file.
But you have to write outside the tags.
Eg:
index.php
<?php
echo "Helloooooo";
?>
<script>
function TestingMyFirstScript()
{
alert(1)
}
</script>
Javascript will execute in a PHP file but not inside of a PHP block. It executes in the server, yes and anything coming from PHP should be printed out to see. You should have the JS code outside of the PHP block and it can be anywhere in the page e.g.
It depends how to mix/match the code but of course keep it clean and easy to read (and debug).
<?php
// code here
?>
<script type="text/javascript">
// JS here
</script>
<?php
// some more code here
?>
Answer to both of your question is that you dont have to create a separate html file to execute your JS code. You can have HTML, JS, and PHP code in the same file. PHP code inside the PHP tags will be processed on the server and replace with HTML. The server generated HTML will be combined with other HTML present on the .php file and sent to the browser as one HTML.
There must be some error in the JS code which is causing the script to fail.
<?php
require_once blah blah
call_my_php_functionBlah();
?>
<script type="text/javascript">
// some known-good javascript code that displays an image
</script>
<?php
// other php code
?>
Most of the above comments should help you with your PHP + JS problem. However, if you are still getting errors with your output, try using:
alert("breakpoint 1");
//some code
alert("breakpoint 2");
throughout your Javascript function (it will show you where the code is failing). Good for beginners debugging. Also check out http://www.jslint.com/
I'm having problems embedding php inside an html file.
I first ran in to this problem when I was trying to 'include' a php file inside tags, and thought it was related to css formatting, or something. But now I've broken this down into the simplest php and html possible, with an example from a book that should work, and I'm still getting this problem. Here's the html sample that doesn't work:
<HEAD>
<TITLE>PHP inside HTML tester</TITLE>
</HEAD>
<BODY>
<?php
echo "Hello World";
?>
</BODY>
</HTML>
I'm expecting 'Hello World' to show up in my browsers, but nothing is displayed. When I try to 'view source', I see exactly the text above. I figure that after all the examples of this I've tried, the code is ok, but something is keeping what's inside the from being recognized.
Any suggestions? And thanks for helping me out with what's probably a dumb question.
There is something wrong with your PHP installation. The web server isn't passing requests for PHP pages off to the PHP interpreter.
If you did indeed save the file as an .html file, then your PHP code will never execute because most web servers have their handler mappings set to route only PHP (.php, .phtml, or .inc extensions) files to the PHP interpreter.
Looks like your server is not able to handle php or your server does not know how to handle the file type with - this code is in.
I am having difficulty trying to get modernizer to run with codeigniter.
this is what I've done so far.
downloaded Modernizr and renamed the file modernizr-1.5.min.txt to modernizr-1.5.min.js
put the JavaScript file in the same directory as my header file is: apppath/views/tops/
included the file in my headings <script src="modernizr-1.5.min.js"></script>
included this in my HTML element <html class="no-js">
just to preserve my sanity I put the JavaScript file modernizr-1.5.min.js in my views directory and in the application directory.
I am getting absolutely zero response when I read my page source to see if the has been replaced with the elements that my browser covers the wave modernizr is supposed to work. I have tried this using Firefox and chrome as far as reading the source.
any suggestions? Thank you in advance
why would you put javascript files ito the view folder of your app? i would rather use this path "/media/javascripts/modernizr.js" and then <script src="/media/javascripts/modernizr.js"></script> link it like that. the view folder is only for templates. other thing is that you will never see the changes that javascript does to your page in the pagesource. because it only shows you what html the browser received. and javascript starts to work after the browser received the html. you need to install firebug to see the "live" dom.