PHP in html not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
idipous
Forum Newbie
Posts: 2
Joined: Thu Oct 23, 2008 12:02 pm

PHP in html not working

Post by idipous »

Hello,

Why this code is not working in my vista box? I have written it my self and I have used it many times without problems.

Code: Select all

<td><input type="text" name="lname" id="lname" value="<?$fields["lname"]?>"size="50" size="50"/></td>
The problem is that on the web page I get the the value <?$fields[ filling the text box and next to it I get "size="50" size="50"/>


It drives me crazy. I have used it many times. Anyown any ideas?
This code is working properly in a server running linux as we speak.

Thank you
idipous
jmaker
Forum Newbie
Posts: 16
Joined: Tue May 21, 2002 11:13 pm

Re: PHP in html not working

Post by jmaker »

I believe if you have short tags enabled then it needs to be this.

Code: Select all

 
<?=$fields['lname']?>
 
bhushanahirrao
Forum Newbie
Posts: 5
Joined: Wed Nov 05, 2008 6:39 am

Re: PHP in html not working

Post by bhushanahirrao »

You can also write # <?php echo $fields['lname']; ?>
idipous
Forum Newbie
Posts: 2
Joined: Thu Oct 23, 2008 12:02 pm

Re: PHP in html not working

Post by idipous »

The
jmaker wrote:#
# <?=$fields['lname']?>
is the one that is not working strangenly. I didn't type the "=" my bad. Still I don't know why. If I use echo instead of "=" it works. Any ideas?

Thanks
idipous
Griven
Forum Contributor
Posts: 165
Joined: Sat May 09, 2009 8:23 pm

Re: PHP in html not working

Post by Griven »

You have Short Tags turned off, which is why <?= isn't working.

It's best to use <?php, just in case you have to move your code to a new server that doesn't have Short Tags turned on.
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: PHP in html not working

Post by ERdman »

In the past I have primarily used php as cgi scripts, and I am now trying to learn how to embed php in html. I am having trouble, even with basic implementation. Ultimately, I would like to use encrypted session_ids as tokens that are submitted with forms to prevent XSS attacks on my site. I have tried using all the following *basic* methods (within the body of the html page), to no avail:

<? echo "Hello world"; ?>

<?php echo "Hello world"; ?>

<script language="php"> echo "Hello world"; </script>

It just gets ignored, and there is blank text on the page when viewed in a browser (IE6). I use PHP scripts all the time without problems and php.ini is configured normally. Do I need some special headers to use php within html? Am I missing something obvious here? Any help is appreciated. Sorry if this is a total noob question, but I am getting desperate here. :banghead:
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: PHP in html not working

Post by ERdman »

OK, I might have at least *partially* figured out the problem. Here is a new question: do you have to use the .php extension for the page name if you are using php (embedded) scripts? If so, that seems pretty clunky. What if you wanted to use multiple script languages within html? OK, before I get too upset I will wait for a reply. Thanks. :?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP in html not working

Post by Jonah Bron »

Always use <?php echo $something; ?>, because as Griven said, your script will work on with all *configurations. Unless you change your httpd.conf, yes, it must have an extension of .php
ERdman wrote:<script language="php"> echo "Hello world"; </script>
If I had a cup of coffee, I would have blown it out my nose. My apologies. The <script> tag is for JavaScript. PHP is processed by the server, and the server **ignores HTML. PHP just processes anything between the <?php and ?> tags.

* Providing there are not other configuration specific variables that come into play.
** Unless other software is installed that is configured to do so.
ERdman
Forum Newbie
Posts: 7
Joined: Mon Nov 15, 2010 8:39 am

Re: PHP in html not working

Post by ERdman »

Jonah,
Thanks for your reply and sorry for the coffee-through-the-nose reaction. I realize php doesn't work like javascript and doesn't execute on the browser side, I was just desperately trying to figure out what was going on. I ended up changing the htaccess file to parse html pages for php, but i realize I could just as easily change the extension to .php. Problem was that I need php to work in my index.html file, and didn't want to redirect to a .php page. I posted the inquiry on a new thread:

Re: Running PHP within HTML pages

Thanks again, Erd. :)
Post Reply