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.
Related
I have a file with .php extension.
I am following a tutorial and it has both and in a same file.
So the file looks something like:
<?php
?>
<!DOCTYPE html>
<html>
</html>
I was wondering which gets executed first?
BTW, this is a sidetrack to this problem.
This particular file has to do with preventing CSRF attack.
Inside HTML, there is a PHP snippet that looks like
<?php Token::generate() ?>
in one of the hidden text fields.
I am not sure why he put both front end and back end in one file.
Is this the necessity to do CSRF prevention?
Thanks in advance.
PHP gets executed on the server, output of this execution is a HTML code.
The browser takes the HTML code and displays it.
I think the execution is based on the order written in the file. Since PHP is on the top, PHP gets executed first.
I don't know why. I'm using php7.0.1,apache2.4.18,mysql5.6.28. My php code is like
<?php
echo "hello world";
?>
and I find it turns out to be this in the browser
<!--?php
echo "hello world";
?-->
<html><head></head><body></body></html>
EDIT
I've uploaded the related files to https://github.com/franklee0817/publicFiles
Your server is clearly not parsing PHP code so PHP tags reach the browser. If you see the actual source code (each browser has a different menu item but keyboard short-curt is often Ctrl+U) you'll see your raw PHP source code. However, if you use the DOM tree provided by your browser developer tools you'll the result of parsing and fixing the HTML tag soup. Applying workarounds to invalid tags is left to browser discretion; yours have decided to entirely omit <?php ... > and wrap it into a comment tag.
I find out what's wrong with my server. The php code is not actually running with php. The apache give the php code straight to browser. everything works properly after I add the below line in httpd.conf.
AddHandler application/x-httpd-php .php
The handler for php was missing. It should be as a default line in httpd.conf, I don't know why it was missing. But after this. Everything back on line. Thank you guys.
I'm creating a LAMP stack and just started learning PHP. I'm having trouble with my Hello World program. On my server I have a file called index.html. This is the contents of that file (which is basically copy pasted from an online guide):
<html>
<title>HTML with PHP</title>
<body>
<h1>My Example</h1>
<?php
echo '<p>Hello World</p>';
?>
<b>Here is some more HTML</b>
</body>
</html>
When opened on a web browser, I expect my output to look something like this:
My Example
Hello World
Here is some more HTML
Instead, it looks like this:
My Example
Hello World
'; ?> Here is some more HTML
Why is that extra "'; ?>" there? I'm probably making a simple mistake. I've tried accessing index.html on chrome, firefox, and safari, same result each time.
PHP does not work in HTML files, try renaming it to index.php
Apart from that the code is fine (maybe some best practices like separating HTML and PHP, but you'll get to that later)
The reason you don't see the whole PHP code on your screen is because the browser is trying to parse the element <?php *** ?> as if it where valid HTML (like <span>). If you check the html-source, you will see all of your code.
As someone in the comments below mentioned, there are ways to make PHP work in HTML. You should not do this, unless you are very aware of what you are doing. It is not a default setting and it should stay that way. If you should be able to use PHP in HTML files, .phpfiles would not have to exist.
When you expand your knowledge, you will start separating HTML files from PHP files and make PHP include templates. Two separate files for two separate goals
Rename the file to index.php.
In addition to this, the PHP code must be interpreted by a WEB SERVER(Apache in your case).
For example(if you are using XAMPP on Windows), and your php file is present in the webcontent directory of your server's document root(htdocs), then type the following in your browser
http://localhost/htdocs/webcontent/index.php.
Like others said, you must rename index.html to index.php, but if you must use index.html, put the line below into your .htaccess file, so you can process .html files as .php.
AddType application/x-httpd-php .html
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">
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/