-1

I have that code:

function saveData(position, map) {
var latlng = marker.getPosition();
var late =  latlng.lat();
var longe = latlng.lng();
    <?php
    require("sql.php");
    session_start();
    $name = 'prova';
    $address = 'temare';    
    $type = 'bar';
    $late = "<script>document.getElementByID('late').value</script>";
    $longe ="<script>document.getElementByID('longe').value</script>";

I need to get late and longe variables in PHP but this doesn't work, anyone knows how I can do it?

Roger Coll
  • 51
  • 1
  • 8
  • where's the html for this and did you wrap the js in script tags? the question is unclear and how you're using this? on a server? as `file:///`? – Funk Forty Niner Mar 26 '17 at 20:15
  • 1
    A request must be sent (including the variables) as GET or POST so the PHP can process their values. – Hossam Mar 26 '17 at 20:16
  • You can't use javascript variable in php. Because js is client side and php is server side. if you want to do any logic with the js variable, you should use ajax. – siddiq Mar 26 '17 at 20:18

2 Answers2

1
<script type="text/javascript">

<?php $abc = "<script>document.write(late)</script>"?> 
<?php $def = "<script>document.write(longe)</script>"?> 

</script>
S M Iftakhairul
  • 1,120
  • 2
  • 19
  • 42
1

You can't access javascript variables using PHP as JavaScript is executed on client side ( browser ) and PHP on server side.

The best what you can do is send data from those variables as request to server and read them, for example using jQuery ajax request:

Good example you can find here: jQuery Ajax POST example with PHP

Or you can use this:

javascript

function sendData(variable1, variable2) {
    $.ajax({
        type: 'post',
        url: 'myphpscript.php'
        data: 'var1='+variable1+'&var2='+variable2
    });

myphpscript.php

<?php
$variable1 = $_POST['var1'];
$variable2 = $_POST['var2'];
Community
  • 1
  • 1
Kamil
  • 990
  • 1
  • 7
  • 24
  • This seems the most resonable answer but in the function sendData how I call the myphpscript.php? – Roger Coll Mar 26 '17 at 20:32
  • I tried what you post but it seems it go to myphpscript – Roger Coll Mar 26 '17 at 20:33
  • You must have included jQuery framework in your HTML, URL is the path where your php script is located from root eg.: index.html app/myphpscript.php will be url: 'app/myphpscript.php' as index html is in the root. – Kamil Mar 26 '17 at 20:35
  • Yes, I have included jQuery framework but now my index.php doesn't load anything. – Roger Coll Mar 26 '17 at 20:43
  • Thanks Kamil for everything I keep trying – Roger Coll Mar 26 '17 at 20:44
  • Now loads the page but it still doesnt get the good value, here's the code: – Roger Coll Mar 26 '17 at 20:53
  • function placeMarker(position, map) { var marker = new google.maps.Marker({ position: position, map: map }); var latlng = marker.getPosition(); var late = latlng.lat(); var longe = latlng.lng(); $.ajax({ type: 'post', url: 'myphpscript.php', data: 'var1='+late+'&var2='+longe }); } – Roger Coll Mar 26 '17 at 20:53
  • I don't see where you calling placeMarker, you must call function place marker before you sending request. Also keep in mind that request might be async, so first you have to check if placeMarker ( google maps ) recieved data before you sending them. – Kamil Mar 26 '17 at 21:00
  • I have that call: – Roger Coll Mar 26 '17 at 21:04
  • google.maps.event.addListener(map, 'click', function(e) { placeMarker(e.latLng, map); }); – Roger Coll Mar 26 '17 at 21:04
  • How you would check if placeMarker recived the data? Thanks again – Roger Coll Mar 26 '17 at 21:05