Pass variable from PHP into HTML [duplicate] - php

This question already has answers here:
How do I add PHP code/file to HTML(.html) files?
(12 answers)
Closed 7 years ago.
I've got a simple .php file with Rock, Paper, Scissors game (some schoolwork) that tracks your score and a .html file with buttons to choose from R/P/S.
Score is tracked via session variable, is there a simple way to pass the value of this variable and display it on the html page?

You should generate your HTML page from PHP file too. That's how php works. That way you can print out PHP variables. HTML is no programming language - it's just markup language...like ...hmmm..XML.

You can add addhandler in .htaccess for html files to treat it as php file, see example:
AddHandler application/x-httpd-php .html .htm
but instead you can, avoid above and change the file extension from .html to .php you can pass it as query string GET, see example below:
http://example.com/myphppage.php?key=value
or you can access session by adding session_start() at starting of page

If you save your .html file as a .php file, you can do this quite easily by adding
<?php echo $_SESSION['score']; ?>
into your HTML. For example:
<p><?php echo $_SESSION['score']; ?></p>

Related

is php embedded in html or is html embedded in php?

I have read on many sites and documents saying like php is embedded in html.
This is ok to understand but this statement is bit confusing for me.
If i have .html file and if i used php code for eg. following line:
<h1> <?php echo "This is PHP"; ?> </h1>
It wont work. But if i used same line of code in .php file it outputs the result.
So my confusion is if we are putting php code in .html file it is not giving results but still we are saying php is embedded in html.
Why cant we say html embedded in php and not php is embedded in html?
Now this line also outputs the same if it is used in .php file
<?php echo "<h1> This is PHP </h1>"; ?>
<h1> <?php echo "This is PHP "; ?> </h1>
Now here the file is .php so we can say we are putting html code in .php file, so if i say html is embedded in php is it right or wrong?
I would say this distinction is not right and not wrong, it's just useless...
You write a .php file, with some php code and some html inside.
Web server parses your file, interprets and executes php code, combines it with html, and produces an html page, which is sent to the requesting browser.
Thant's it... :-)
It depends on your server configurations if you want .html files to be treated as an php file(if it not already does) add this on your .htaccess file
<FilesMatch "\.html$">
SetHandler application/x-httpd-php
</FilesMatch>
Now these is an example of PHP in HTML
<input type="text" name="<?php echo $name;?>" value="<?php echo $value;?>"/>
And this is the example of HTML inside PHP
<?php echo "<input type=\"text\" name=\"$name\" value=\"$value\"/>";
You can not say html is embedded in php only if it is inside .php file it depends on your code and not your file extension .

PHP placement within HTML page? [duplicate]

This question already has answers here:
PHP in HTML page, Super Quick Advice
(3 answers)
Closed 9 years ago.
I have asked a question leading up to this earlier. Thanks again for your help.
I have updated my .htaccess file in my root directory with the following:
AddType application/x-httpd-php .html
Where within my .html page should I place the php tag?,
I still cannot seem to get my .html page to load the .php script.
If you have followed this guide correctly, then according to the article, you should be able to execute php by simply opening a php tag.
From the article:
Things to watch out for:
If you have an existing .htaccess file, add this to it, do not overwrite it or other >settings may stop working! Always be very careful with your .htaccess file and ask your >host if you need help
Anything in your .html files that starts with your file for some other reason (an XML tag for example) you will need to echo these lines >to prevent errors. For example:
<?php echo '<?xml version="1.0" encoding="IUTF-8"?>'; ?>
Where within my .html page should I place the php tag?
The answer is you can place your php content anywhere in your html page as long as it is inside <?php ?> tag.
even if you have done correct modification to .htaccess file and still your php content is not getting parsed than check whether you are connected to server or not.

PHP INCLUDE function inside an HTML file

I have an HTML file that has a rather long navigation menu inside of it. I want to take that menu out of the HTML and place it into an external PHP page and then call it with
<?php include 'navigation.php'; ?> in the HTML file.
I have tried just adding this into the HTML file but it doesn't display anything as well as no errors on the page.
What do I need to do (if it's even possible) to keep the files HTML and use the php require function?
Add this in in your httpd.conf and then you can process PHP code on HTML pages
AddHandler application/x-httpd-php .php .html
Q: Did you give the page a .php suffix? That should be all you need to do.
Remember the way PHP works - you basically "embed" your PHP code in an HTML page, and the server executes the PHP before it serves (the rest of) the HTML.
But in order for PHP to "see" your code, you need to make sure your "HTML page" has a .php suffix.
As a crude workaround, you can add ".html" to the list of file suffixes that PHP will parse.
But this could cause other things to break.
If you want to embed PHP code in your "index.html", the best, cleanest approach is to simply rename it "index.php".
IMHO...

php how can i include internal pages by url? [duplicate]

This question already has answers here:
Include another HTML file in a HTML file
(41 answers)
Closed 8 years ago.
for example my website is "myweb.com". how can i do opration like following.
include(http://myweb.com/file);
this is an internal URL.
for example i want to include
http://myweb.com/process.php?action=update
this is not a file
"?action=update"
thus how can i do this operation?
To include pages with PHP, simply put the following code in a.html, where you would like the code to appear:
<?php
include "b.html";
?>
You can't include an HTML document into another HTML document.
You can do that using PHP but the file that includes the other must be a PHP document. In your example a includes b therefore a is a.php while b can stay b.html.
Then inside a.php you can write:
<?php echo file_get_contents('b.html'); ?>
and the content of b.html will be included into a.php.

Get same PHP session ID for both html and php files

I have two files:
html
php
where html file is calling the php file to do something.
Now i would like to get the PHP session ID (php code) into both my html file as well as my php file.
I would like to know how would I do so so that both html file and php file have the same PHP session ID.
Thanks for your help in advance.
You can't have PHP sessions in HTML files. Best to just change the HTML file to a PHP file.
As mentioned in the comments - make sure you start the session at very top of the file before any spaces before the opening php tags.
To retrieve the session ID use php function session_id() but if the both files are on same domain you just need to call session_start at the very top and it'll just use same session across.
EDIT
To answer your qs in comments below -
Yes, a PHP file can just have HTML code but no PHP code at all or add PHP where required. e.g.
Myfile.php
<html>
<head> ... </head>
<body>
<h1>some title</h1>
......
......
Go to next page (<?php echo $_GET['next_page']; ?>)
......
</body>
</html
so you just open and close php tags where you needed php stuff to go.
To print session ID - just use somewhere in the HTML of PHP file.
Yes, session_id() will give you the same session ID in php2 file - again, make sure you call session_start function at the very top in the php2 file too.
If you have access to your apache configuration, or a simple .htaccess file, you can tell Apache to handle php code inside of an .html file. You can do this by creating an .htaccess file (remember the . (dot) as the first character in that filename) on the document root of the site (probably public_html/) and putting this into it:
# Add this to public_html/.htaccess file
AddHandler application/x-httpd-php .html
AddHandler application/x-httpd-php .htm
You should be able to reload the html page and your PHP code will run great.

Categories