Using a PHP variable in JavaScript for Google Maps V2

I am currently on Google Map Version 2 (sorry, using the old version and please do not offer me to use V3 because this is our requirement). Basically, this page should allow the user to add a place and choose the type of place (for example, a restaurant, bar, school, etc.). This information will be added to the database and will be used for switching. Adding space now works and requires redirecting to another php page (testmap.php) so that it displays markers on the map. But switching requires information from the type selected in the drop-down list. My problem is that I donโ€™t know how to โ€œbindโ€ a type to each marker. I donโ€™t know how to transfer data from the Type drop-down list.

PS

  • The fourth and last line is the code in which the address data is passed in my addNewMarker onSubmit function.

  • The PHP code above it is a drop-down list that contains the possible values โ€‹โ€‹of the "types" as described above.

  • In conclusion, I need the actual codes on how to get the marker type from the drop-down list and migrate along with the addNewMarker function so that testmap.php can use the values โ€‹โ€‹in the database and can be easily switched.

Thank you so much for those who will help me!

<script type="text/javascript"> if(GBrowserIsCompatible()) { map_compatible = true; } /* Initialize the map this will be called when the document is loaded from: <body onLoad="initialize_map();"> */ function initialize_map() { if( map_compatible ) { } function addNewMarker(newAddress){ var set; var lat; var longi; if(set==null){ set=1; geocoder = new GClientGeocoder(); geocoder.getLatLng(newAddress, function(point){ if(!point){ alert(address + " not found"); }else{ lat = point.y; longi = point.x; alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi); default_address.push([latitude,longitude]); location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress; } }); } } function getType(type){ //markertype = type; document.write("Hello"); } </script> </head> <body onLoad="initialize_map();"> <div id="main-wrapper"> <div id="main-padding"> <div id="map_canvas"></div> <form name="markertype_form" method = "POST" action = "addMarker.php" class="form"> <?php @$submit = $_POST['view']; $selectedtype = ''; if(isset($_GET)){ $connect = mysql_connect("localhost","root","abc123"); mysql_select_db("mapping"); $query="SELECT id,typename FROM markertypes ORDER BY typename"; $result = mysql_query ($query); echo "<select name='types'>"; $types = strip_tags(@$_POST['types']); echo "<option disabled>---------------------Select---------------------</option>"; while($nt=mysql_fetch_array($result)){ $selected = false; // check if the current value equals the value submited if($_POST['types'] == $nt['id']){ $selected = true; $selectedtype = $nt['typename']; } // show selected attribute only if $selected is true echo "<option value='{$nt['id']}' ". ($selected ? "selected" : "") .">{$nt['typename']}</option>"; } echo '</select>'; echo "</select>"; echo "<input type='submit' name ='view' value='View Details'>";// Closing of list box echo '<br>'; } ?> </form> <form name="direction_form" onSubmit="addNewMarker(this.new_address.value); return false;" class="form"> Add markers? Enter the address and we will mark it for you: <input type="text" name="new_address" class="form-field" /><br /><br /> <input type="submit" value=" Mark this place! " class="form-submit" /> </form> 
+4
source share
1 answer

If you put the identifier in the type select box in your php, you can add some lines of code to your javascript above.

The following assumes that the types selector has an identifier of the marker type:

 function addNewMarker(newAddress){ var set; var lat; var longi; var e = document.getElementById("marker-type"); var mtype = e.options[e.selectedIndex].value; if(set==null){ set=1; geocoder = new GClientGeocoder(); geocoder.getLatLng(newAddress, function(point){ if(!point){ alert(address + " not found"); }else{ lat = point.y; longi = point.x; alert("The latitude of " + newAddress + " is " + lat + " and longitude is " + longi); default_address.push([latitude,longitude]); location.href="testmap.php?lat="+lat+"&longi="+longi+"&set="+set+"&newAdd="+newAddress+"&type="+mtype; } }); } } 

Note that in the AddMarker function, I added a new variable for "mtype", which selects the selection field and receives the current value of the parameter.

Then below, where the API calls testmap.php, I added a type selector and gave it a value for the variable "mtype".

Later in the testmap.php code, you simply follow using:

 $_GET['type'] 
+3
source

Source: https://habr.com/ru/post/1399722/


All Articles