0

Following code is saved as html file:

<?php
  $IP = 4;
?>
<textarea name="comments">$IP</textarea>

I want to access the value of php variable in html tags. Example I want the value of IP to be displayed in textarea.

3
  • do you saved your file with .html extension?? Commented Feb 9, 2012 at 8:59
  • 4
    I suggest you find a PHP tutorial and start reading it if this is a genuine question .... Commented Feb 9, 2012 at 9:00
  • The answer from Jaitsu will allow you to do what you want. Commented Feb 9, 2012 at 9:08

7 Answers 7

5

You can execute .html files as .php files by adding a line to your .htaccess file..

AddType application/x-httpd-php .html

And obviously use the <?php echo $variableName; ?> style syntax

Sign up to request clarification or add additional context in comments.

2 Comments

I am using wampserver and did the same what u said but still no result is displayed(<?php echo $IP; ?>) .
Worth mentioning you need AllowOverride FileInfo in your apache config to do this.
3

use this:

 <textarea name="comments"><?php echo $IP; ?></textarea>

3 Comments

i tried the ablove code but its not working when file is saved .html this code works file with .php file
Follow what jogesh_p below said. Save the file with the extension .php so your webserver knows to parse the PHP code
Otherwise you will just see the verbatim output in your browser, unparsed.
1

Just put it with PHP

<textarea name="comments"><?php echo $IP; ?></textarea>

Comments

1

it is not possible to access in html, simply save the page in '.php' and if you want to change url, means .html instead of .php then use .htaccess

2 Comments

can you please explain in detail please
this isn't quite true, @Sjan checkout my answer
1

Here is a short tutorial on using PHP in HTML pages. Note that in order to use PHP in an HTML page, you need to save the page with a .php extension.

to answer your question above, you need to do the following:

<?php
$IP =4;
?>

<textarea name="comments"><?php echo $IP;?></textarea>

Comments

1
<?php $IP =4; ?>
<textarea name="comments"><?php echo $IP;?></textarea>

Comments

0
<textarea name="comments"><?php echo $IP ?></textarea>

Extra cool stuff (for free!!!):

http://php.net/manual/en/install.php
http://www.php.net/manual/en/tutorial.firstpage.php
http://devzone.zend.com/6/php-101-php-for-the-absolute-beginner/
etc, etc...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.