0

Hi I am a newbie to web development. I am trying to display echo message from a php file in html page.

PHP File:

<?php
    echo "Hello";
?>

Other file:

<form method="get" action="latest.php">

</form>

Note: These both are two different files

I am completely new to this. I don't know where I am going wrong.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
user2727874
  • 37
  • 1
  • 3
  • 8

7 Answers7

4

Normally, you can only execute PHP code in a file which has .php extension because your webserver is setup like that for PHP. However you can simply tell your web server to parse your HTML files as PHP too and then you can run your PHP code in an HTML file wherever you want. Now assuming your Web server is Apache.

Step 1:

Create a file named .htaccess and place the following code in it and then place this file in your root directory for that website

AddType application/x-httpd-php .html

Step 2:

Go to Your HTML file and add an include statement where you want your PHP file to be included

<form method="get" action="latest.php">
<?php include ("yourPHPfile.php"); ?> 
</form>

Step 3:

All done, now go check output in browser.

Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • i dont know why u are getting the downvote,i think thats because people misunderstood the question. – R R Nov 13 '13 at 06:48
  • Don't worry, there are many trigger happy people over here who love to vote without knowing what the question is. Let them waste their time :) – Hanky Panky Nov 13 '13 at 06:57
1

Just put the code together:

<form method="get" action="latest.php">
<?php echo "Hello"; ?>
</form>

Everything inside the Tags will be interpreted as PHP, no matter where it is. But make sure the ending of the file is .php!

Realitätsverlust
  • 3,941
  • 2
  • 22
  • 46
1

You can't execute php codes in a file ending other than .php. But you can have HTML code displaying in a .php file. So I would recommend you change the extension to .php.

You can simply have

<?php 
$name = "Harry Potter"; 
?>
<h1>Hello, <?php echo $name;?>!</h1>

Also you need a web server software and php processor. WAMP or XAMP may help you on that.

I am editing my answer since some are really picky. In general you can't execute php in files which dont have the .php extension. But with some tweaks and line changes you can. I am just stating that you can't since, you mentioned that you are new to web development and I would like to make things simpler rather than confusing

Ela Buwa
  • 1,652
  • 3
  • 22
  • 43
  • May be you didn't notice that he mentioned to be a newbie. While you can definitely do it, I would not want to confuse him even more fiddling with apache and php – Ela Buwa Nov 13 '13 at 06:30
  • Well he definitely would have to use something along those to run php in a local developing machine. Unless you would like to install apache, php and mysql seperately and go in to configuring ini files. – Ela Buwa Nov 13 '13 at 06:32
  • 1
    I certainly remember how I started php, but you seem like you were a hotshot the moment you began php. We are trying to help the OP not confuse him configuring servers the moment he is starting out – Ela Buwa Nov 13 '13 at 06:35
  • oh!! OP change quetion, he has a two diff. file. – DS9 Nov 13 '13 at 06:35
  • If OP has two files, you can include the second php file (keep in mind what I said about extensions). . – Ela Buwa Nov 13 '13 at 06:38
  • Ok let me remove all this noise from your answer and add to my answer. – Hanky Panky Nov 13 '13 at 06:41
  • @Hanky웃Panky I was just trying to make things simpler for him at the begining. Your answer seems to explain it well too. – Ela Buwa Nov 13 '13 at 06:43
1

May be try this..

<form method="get" action="latest.php">
    <?php include('php_file.php'); ?>
</form>

Please note that other file should also be a .php file. if it is an html file you either need to change it to .php or you need to make ajax request.

zzlalani
  • 22,960
  • 16
  • 44
  • 73
1

If you want to execute php code in .html file extension than u need to do some htaccess changes.since file extension plays a major role ,it only tells the browser how to handle or parse a particular page.

You can create a .htaccess file at the root folder of your website and in the htaccess file add this line:

AddType application/x-httpd-php .html .htm

If you only plan on including the PHP on one page, it is better to setup this way:

<Files yourwebpage.html>
AddType application/x-httpd-php .html
</Files>

This code will only make the PHP executable on the yourpage.html file, and not on all of your html pages.

If you are ok with changing your file extension to .php than your work will be quite easy.you can simply include a php code between html code.like this

<form method="get" action="latest.php">
<labe><?php echo "hello" ?></label>
</form>
R R
  • 2,999
  • 2
  • 24
  • 42
0

Its so simple you can just put php tag and write php code even inside the html

<form method="get" action="latest.php">
   <div><?php echo "Hello";?></div>
</form>
zzlalani
  • 22,960
  • 16
  • 44
  • 73
Veerendra
  • 2,562
  • 2
  • 22
  • 39
0

Whenever you echo something, it is printed on html page only.

If you want to echo inside a particular html element, just put the code there. For example,

<html>

<body>

<div class="myDiv1"><?php echo "This is myDiv1"; ?><br></div>
<div class="myDiv2"><?php echo "This is myDiv2"; ?></div>

</body>

</html>

Hope this helps...

Adi
  • 407
  • 1
  • 4
  • 13