Perform one page function on another

Asked

Viewed 226 times

0

I have a question regarding my project. I have a button in a page "x" that when clicking it checks the geolocation in Google maps (fills the inputs with the coordinates). So far so good, I need to do this function also in a page "y" with the same scheme: button; when clicked catch the value of two input fields and then mark on the map. Only that the map is in the page "x" and the button on page "y" and the script on the map in "script.js".

I can’t think of any way to do that, because I don’t have very much knowledge. I saw that the possibilities in an AJAX of life would be right, but I do not have much knowledge.

To help, follow images: index.php equipamentos.php

I’d like your help with that doubt, if there’s a way to do it and an example of how to do it.

Briefly, it would be: run button on page y -> the function is read from a Function in the script file. js where is the map and the functions -> then performs this function on page x and marks on google maps api the location.

  • This page is where. Explain more about where the pages are.

  • So you have index.php where the map is loaded (coming from . js) the inputs and the find button. When you fill in the inputs and press "find" google maps positions a marker in the api according to the input values. So far, okay, just that I need to do this on the.php equipments page where I refer the same file. js with the same button and even referencing, it does not receive Function values, let alone mark in index.php the location.

  • I put images to improve understanding.

  • Well. Changing information between pages is somewhat complicated. But it’s possible. I just haven’t seen myself doing it. An average solution to this problem would be to render this crud in a modal and not in a Poup-up. With the modal you will be in the same document can change any action or variable with javascript.

  • I understood, the problem is that I only left popup because the map contains bookmarks with js de window.open based on the information in the database.. It would have to be really popup and try to find a way for two pages to exchange information

1 answer

1

You can use the Localstorage to save the coordinates and on the next page get them. Localstorage is a storage space in the user’s browser.

Ex:

// Salva na página anterior
localStorage.setItem("latitude", "-450.56464984");
localStorage.setItem("longitude", "-38.89748914");

// Recupera na próxima página
document.getElementById("campo-latitude").innerHTML = localStorage.getItem("latitude");
document.getElementById("campo-longitude").innerHTML = localStorage.getItem("longitude");
  • Would that be a good thing, but it doesn’t matter in the opening order of the pages? Does the.php device open after index.php? And in the case of the coordinate value, it comes from a database query, in the case for example: $Row['latitude'], in the case: localStorage.setItem("latitude", "latMap") -> (which is the ID of the Hidden input that stores the database coordinates)

  • I believe the whole domain has access to the same localStorage.

  • And how can I send this to the input field of index.php with the click of the button on the equipment page.php? Sorry for the obvious question, but I’m not very good with javascript anyway.

  • Thus, in the solution I gave, you save it in the user’s browser and end, you can recover this value on any page. And you would not pass the ID pro localStorage.setItem(), you save the value of the field to a variable, and then put this variable in setItem(), like this: var coordinate = Document.getElementById("latMap"). val(); localStorage.getItem('latitude', coordinate);

  • I don’t know if I did something wrong but, appears on the console that the Function mandaCoordenada() is not defined. Follows the lines I made: Na index.php pus :Document.getElementById('latMap'). innerHTML = localStorage.getItem('latitude', latitude); Document.getElementById('lngMap'). innerHTML = localStorage.getItem('longitude', longitude);

  • On the.php equipment I put: Function mandaCoordenadas(){ var latitude = Document.getElementById('latMap'). val(); var longitude = Document.getElementById('lngMap'). val(); window.localStorage.setItem('latitude', latitude); window.localStorage.setItem('longitude', longitude); });

  • These are the inputs for the.php: <input type="Hidden" name="latMap" id="latMap" value ='". $Row['lat'] ." '> <input type="Hidden" name="lngMap" id="lngMap" value ='". $Row['lng'] ."'>

  • The hardware button.php: <button id="Submit" onclick="mandaCoordenadas();" class="btn btn-Sm btn-Primary"> <i class="fas fa-map-Marker-alt"></i></button>

  • And the excerpt from the script.js : Function geocodeLatLng(geocoder, map, infowindow) { var input1 = Document.getElementById('latMap'). value; var input2 = Document.getElementById('lngMap'). value; var latlng = {lat: parseFloat(input1), lng: parseFloat(input2)}; geocoder.geocode({'Location': latlng}, Function(Results, status) { if (status === 'OK') { if (Results[1]) { map.setZoom(19); map.setCenter(latlng);

  • var Marker = new google.maps.Marker({ position: latlng, map: map, url: 'index.php', icon: 'http://maps.google.com/mapfiles/ms/icons/green-dot.png' }); infowindow.setContent('Temporary bookmark'); infowindow.open(map, Marker); } Else { window.Alert('No results found'); } } Else { window.Alert('Geolocation failed due to: ' + status); } Marker.addListener("dblclick", Function(){ Marker.setMap(null); }); }); }

  • I saw in your comment above that you are using window.open, so it is better to pass the parameters through the URL, eg windows.open("http:/localhost/equipamentos.php?latitude=" + latitude +"&longitude=" + longitude); to get the data you use $_GET['longitude'] direct to PHP, or Javascript as follows: https://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript

  • Okay, but could I change that variable every time on the link? Because it would have to click on the button and present the variable of each equipment in case the user wants to see where is one by one on the map

  • It is possible to change every time yes

  • I get it, but doesn’t it have to be in index.php? because I also need the.php hardware button to send this to index.php (in input fields) and do not need to search automatically, just sending this to input fields helps me a lot.

  • Yeah, it could be from anywhere to anywhere

  • I get it, I just need to know how to pass parameters to the url dynamically to make it work.. Have you ever done this before or seen something like this somewhere to advise me?

  • windows.open("localhost/equipamentos.php?latitude=" + latitude +"&longitude=" + longitude); <<< so is dynamic, ta concatenating the URL with the variables.

  • where it is written latitude and longitude, are variable

Show 13 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.