PHP not playing nice with HTML

I am having fits with placing a snippit of php into a html file. The php works great by itself but when placed into the html it fails!?!?!
The php I’m placing:[php]<?php
require(“dealer_map_dbinfo.php”);
mysql_connect($local_host,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$state_name_query = “SELECT state_name FROM states_list”;
echo “”;
$state_name = mysql_query($state_name_query) or die(mysql_error());
$dropdown = “”;
while($row = mysql_fetch_assoc($state_name)) {
$dropdown .= “\r\n<option value=’{$row[‘state_name’]}’>{$row[‘state_name’]}”;
}
$dropdown .= “\r\n”;
echo $dropdown;
mysql_close();
?>
[/php]

and the html with the php in it:[code]

Dealer Locator <?php require("dealer_map_dbinfo.php"); mysql_connect($local_host,$username,$password); @mysql_select_db($database) or die( "Unable to select database"); $state_name_query = "SELECT state_name FROM states_list"; echo ""; $state_name = mysql_query($state_name_query) or die(mysql_error()); $dropdown = ""; while($row = mysql_fetch_assoc($state_name)) { $dropdown .= "\r\n{$row['state_name']}"; } $dropdown .= "\r\n"; echo $dropdown; mysql_close(); ?>

Locator

Address or Zipcode:
Radius: <select id="radiusSelect">
  <option value="25" selected>25mi</option>
  <option value="50">50mi</option>
  <option value="100">100mi</option>

  <option value="200">200mi</option>
</select>
<input type="button" onclick="searchLocations()" value="Search Locations"/>
<br/>    
<br/>
    </td>
    <td> <div id="map" style="overflow: hidden; width:400px; height:400px"></div> </td>
  </tr> 
</tbody>
[/code]

When I run the html page using xampp or a live “test” page I get the same results:

"; $state_name = mysql_query($state_name_query) or die(mysql_error()); $dropdown = “DROP DOWN SHOWS HERE”; echo $dropdown; mysql_close(); ?> BUTTON SHOWS HERE

appears on the html page with the correct buttons where I’ve indicated USING ALL CAPS

So I tried to add in some start php, end php tags like so:
[php]<?php
require(“dealer_map_dbinfo.php”);
mysql_connect($local_host,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$state_name_query = “SELECT state_name FROM states_list”;
?>

<?php $state_name = mysql_query($state_name_query) or die(mysql_error()); $dropdown = ?><?php; while($row = mysql_fetch_assoc($state_name)) { $dropdown .= ?>
'><?php{$row['state_name']}?><?php; } $dropdown .= ?>
<?php; echo $dropdown; mysql_close(); ?> [/php]

The php code does not show up but all the drop down has in it is '>

Thanks for any help.

It sounds like you are using an .html file extension instead of .php

In order for the server to process the php, it has to have a .php extension.

Please let me know if you are using .php and I will try to look at it in more detail.

jay

DUH!! that was a bone head maneuver on my part… is there a way to get it to work in a .html file?

There is nothing that comes to mind of the top of my head to get it to work with a .html extension but I will do some more digging.

This page has instructions for doing what your looking for but it requires editing the .htaccess file.

Not that I know of.

The server looks at the file extension to determine how to proceed with it. If is sees .php it knows that it contains server side code and will parse it. If it see html it will skip the parser and just output the file as html content.

If you absolutely have to use the html location, let me know, as there are some workarounds.

These include html redirects, .htaccess redirects and iframes. I would avoid them if you can.

If you do decide to go the .htaccess route, MAKE SURE you have a backup of your original .htaccess file before making changes.

Also note that depending on your version of php and how it is installed, there are different ways that this has to be done. There is not one way as the article would imply. Please also see this article and read through the comments before attempting to do it this way: http://www.besthostratings.com/articles/php-in-html-files.html

Looks like I’m going to have to find another way to implement the php.
I did some more digging, it turns out the spot I wanted to place the php and html is actually getting pulled from a MySQL database field from inside another php file. At least now I have the locator 100% working, just have do some playing around to get it placed properly.
Thanks for the advise.

Sponsor our Newsletter | Privacy Policy | Terms of Service