Page 1 of 1
PHP in html not working
Posted: Wed Sep 30, 2009 8:17 pm
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
Re: PHP in html not working
Posted: Wed Sep 30, 2009 9:08 pm
by jmaker
I believe if you have short tags enabled then it needs to be this.
Re: PHP in html not working
Posted: Wed Sep 30, 2009 10:24 pm
by bhushanahirrao
You can also write # <?php echo $fields['lname']; ?>
Re: PHP in html not working
Posted: Fri Oct 02, 2009 12:00 am
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
Re: PHP in html not working
Posted: Fri Oct 02, 2009 1:36 am
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.
Re: PHP in html not working
Posted: Mon Nov 15, 2010 8:48 am
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.

Re: PHP in html not working
Posted: Mon Nov 15, 2010 9:36 am
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.

Re: PHP in html not working
Posted: Mon Nov 15, 2010 8:23 pm
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.
Re: PHP in html not working
Posted: Thu Nov 18, 2010 9:43 am
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.
