0

I'm strugeling with this ptoblem for a while. Need to pass data from JS to PHP on wordpress site. The error: enter image description here

    <script>
function activatePlacesSearch(){
    var input = document.getElementById('shipping_user_location');
    var autocomplete = new google.maps.places.Autocomplete(input);
    var place;
    google.maps.event.addListener(autocomplete, 'place_changed', function(){
        place = autocomplete.getPlace();
        console.log("Latitude: "+place.geometry.location.lat());
        console.log("Longitude: "+place.geometry.location.lng());
        $.ajax({
          url:"https://DOMAIN/wp-content/themes/techmarket-child/functions.php",
          method:"post",
          data:{place:JSON.stingify(place)},
          success: function(res){
            console.log(res);
            alert('Ok');
          },
          error: function(error){
            console.log(error);
          }
        })
    });
}
</script>

Finally the code in responsible for autofill the address, and it works. But just simply can't send the data. On the other end i have the this code. But the issue probably on the JS lv:

function add_lat_lng_to_customer_meta($fields){
$place = json_decode($_POST['exampleJS']);
echo $place;die;
return $fields;
}
add_filter( 'woocommerce_customer_save_address', 'add_lat_lng_to_customer_meta' );

If any additional data is need just write me.

  • If $ is undefined then jquery didn’t load yet. Try wrapping your code with an onLoad handler – Luckyfella Sep 11 at 23:49
  • Hello @Luckyfella thanks for help. OK so before the $.ajax block I added the window.onload = () =>, The error is no longer there but the code from inside the "window.onload" block is not used. – Szelek Sep 12 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.