Nothingbutageek
Nothingbutageek

Reputation: 417

My GoogleMap does not Work in PHP

I've started to work on HTML5 and PHP but i had a problem about Inserting google map inside of my php file. I'm going to put sample codes i've written. Maybe anyone has a solution for my situation.

In my first file which is 'index.html' I have a input and a button which will submit and send input to php file. The code that i've written for this is shown below.

<form action="deneme.php" method="post">
                <input type="text" name="searchtxt" id="searchtxt" placeholder="Write something to search"></br>
                <div align="center">
                <input type="submit" name="locationbtn"  data-icon= "search" data-iconpos="right" data-theme="a" data-inline="true" value="Find Location"></button> 
                </div>
            </form>

In my 'deneme.php' file the codes are just like that.

<!DOCTYPE html>
<head>
<meta charset=utf-8>



<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script>
<link rel="stylesheet" href="css/bgcolor.css" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script>  
<script type="text/javascript">
function TestGeo()
{
     if (navigator.geolocation) 
        {
          navigator.geolocation.getCurrentPosition( TestMap, error, {maximumAge: 30000, timeout: 10000, enableHighAccuracy: true} );
    }
    else
    {
          alert("Sorry, but it looks like your browser does not support geolocation.");
    }
}

var map;
 function TestMap(position)
 {
       // Define the coordinates as a Google Maps LatLng Object
       var coords = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);

       // Prepare the map options
       var mapOptions =
      {
                  zoom: 10,
                  center: coords,
                  mapTypeControl: false,
                  navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL},
                  mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // Create the map, and place it in the map_canvas div
        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        // Place the initial marker
        var marker = new google.maps.Marker({
                  position: coords,
                  map: map,
                  title: "Your current location!"
        });

function error() {
        alert("Cannot locate user");
        }
</script>   
</head>


  <body onload="TestGeo();">
<?php 
    $t=date("H");
    $search=$_REQUEST['searchtxt'];

        if($t<"18"){
      echo ("<br/>Have a good day " . $search . " !");
    }
        else{
      echo ("<br/>Have a good night " . $search . " !") ;       
    flush();
    flush();
        }

      ?>
</div>
            <div id="map_canvas" style="width: 100%; height: 85%;  border-right: 1px solid #666666; border-bottom: 1px solid #666666; border-top: 1px solid #666666; border-left: 1px solid #AAAAAA;" align="center">
    </div>

The main problem is my deneme.php file is not loading google map when i click to Submit button, but i have the input value entered in index.html printed in deneme.php file. I've tried to solve problem by changing button from <input> to <button href="#deneme.php" type="button" name="locationbtn" data-icon= "search" onclick="location.href='deneme.php'" data-iconpos="right" data-theme="a" data-inline="true">Find Location</button> after doing this, I could not get the input in html file printed in php file. But google map works in this case.

Upvotes: 0

Views: 136

Answers (1)

Marc B
Marc B

Reputation: 360842

.html files are generally NOT processed through the PHP interpreter, and just sent out as plain text. If you hit your submit button, and do a "view source" on the location.html page, you'll probably see the raw PHP code embedded in there. Try renaming it to location.php, changing your form to point to this new filename, and try again.

Upvotes: 1

Related Questions