Incorporate ajax into the JavaScript geocoder functionality

After retrieving the latitude and longitude coordinates from the maps, I am trying to send the address to another PHP file using Ajax in JavaScript.

Unfortunately, the Ajax is not working properly for me. I am puzzled as both the Ajax code and the JavaScript maps section have been individually tested and are functioning correctly.

<script type="text/javascript">

        var latlng = new google.maps.LatLng(<?php echo $lat;?>, <?php echo $long;?>);
        var geocoder = geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'latLng': latlng }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                if (results[1]) {
                    var Var_JavaScript = results[1].formatted_address;
                    window.location.replace("insert-fichaje2.php?lat=<?php echo $lat;?>&long=<?php echo $long;?>&status=<?php echo $status;?>&usuario=<?php echo $usuario;?>&direccion="+Var_JavaScript);

                }
            }

        });

</script>

If I replace window.location.replace with:

$.ajax({
            type: "POST",
            url: "insert-fichaje2.php?lat=<?php echo $lat;?>&long=<?php echo $long;?>&status=<?php echo $status;?>&usuario=<?php echo $usuario;?>&direccion="+Var_JavaScript ,
          dataType: "json",
    success: function() {
      alert("Success");
       window.close();

    },
    error: function() {
      alert("ERROR!!!");
    }

        });

then the functionality breaks.

Console error messages:

    Uncaught SyntaxError: Unexpected token '<'
insert-fichaje.php?lat=40.394548&long=-3.5861034&status=Pauza&usuario=liviudiaconu:14 Uncaught ReferenceError: $ is not defined
    at insert-fichaje.php?lat=40.394548&long=-3.5861034&status=Pauza&usuario=liviudiaconu:14
    at geocoder.js:5
    at geocoder.js:2
    at XS (geocoder.js:2)
    at Kp.d [as o] (geocoder.js:2)
    at Object.c [as _lby7xg] (VM87105 common.js:68)
    at GeocodeService.Search?5m2&1d40.394548&2d-3.5861034&7sUS&9ses-ES&callback=_xdc_._lby7xg&key=AIzaSyBIgTmGmP2ccdBJWMJbH8MYHdxB33tb9AU&token=50596:1

Answer №1

After including these two lines of code, my program is now functioning perfectly:

<script src="code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Big shoutout to Quentin for the assistance!

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the reason behind the success of this location?

Curious about the functionality of this particular JavaScript code with its corresponding HTML structure: function getCityChoice() { const location = document.querySelector("form").location.value; console.log(location) } <form name="reserve" a ...

Ways to verify if the flash plugin has been disabled in the Chrome browser

Is there a way to use JavaScript or jQuery to detect if the flash plugin is blocked in Google Chrome? You can check for a disabled flash plugin using the following code: ((typeof navigator.plugins != "undefined" && typeof navigator.plugins["Shock ...

What is the best way to automatically log out a user once their session cookie has expired during an online exam?

While developing an MVC ExpressJS Test app, I encountered an issue with auto-logout functionality for users who stop answering test questions. The error message "Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client" keeps app ...

Strategies for obtaining newly updated data with every request and implementing a no-cache approach in Apollo GraphQL and Angular

Every time a request is made, we need a fresh value, like a unique nonce. However, I am facing issues while trying to achieve this with Apollo's Angular client. My initial solution was to utilize watchQuery with the no-cache strategy: this.apollo.wat ...

Switching to http2 with create-react-app: step-by-step guide

Can someone provide guidance on implementing http2 in the 'create-react-app' development environment? I've searched through the README and did a quick Google search but couldn't find any information. Your assistance is much appreciated. ...

My current issue lies in my ability to successfully retrieve data from the controller using AJAX calls in .NET CORE, however, I am

Trying to pass a string id from the View to the controller for calculations and then return a string back to the view. Although I can receive the return data from the controller, the parameter 'String data' in the action is always null, indicatin ...

When an input in Vue.js registers a @keyup event, it will return to the parent

I am encountering an issue with my input component where pressing enter redirects me to the parent component. Even adding a @keyup event does not solve the problem. Could this be happening because I am in a child component? How can I prevent the enter key ...

A guide on implementing the MVC architecture in a web application with Node.js, Express, and PostgreSQL

I'm struggling with implementing the MVC architecture in my node web app, specifically when it comes to separating the model from the controller. Currently, I have the views properly organized (all my .ejs files) and I consider my app.js as the contr ...

jQuery: Modifying the Style of obj "th" Element

I am looking to update the style of my selection using jQuery. After using this module to retrieve all items, I have a list of cells with new coordinates. //cell list with new coordinates cl = Object {0-0: th, 0-1: th, 0-2: th, 0-3: th, 1-0: td…}, id = ...

JavaScript: Populating an Array with Image URLs Using a Loop

My image gallery project has hit a roadblock and my JavaScript skills are not up to par. Maybe it's time to brush up on the basics with a good book from the library. Here's what I want to achieve: 1. Create an array of URL's for each imag ...

Step-by-step process for adding .env file and hosting React app on Netlify

My React application is currently on GitHub, and I am looking to host it on Netlify. I am uncertain about the placement of my .env file, which holds all the necessary API credentials. ...

Trouble with unproductive downtime in ReactJS and JavaScript

I'm looking for a way to detect idle time and display a dialog automatically after a certain period of inactivity. The dialog should prompt the user to click a button to keep their session active. Currently, when the user clicks the button it doesn&a ...

What is the best way to showcase the chosen items in a dropdown menu?

There seems to be an issue with the selected item not appearing in the input field. export default function BasicSelect() { const [sortBy, setSortBy] = useState(""); const [condition, setCondition] = useState(""); const [delivery, ...

Steps for filtering a table with Vue and Element-UI

I am currently working with Vue and Element UI, and I have a requirement to display only elements in my table that have a status of 0. To retrieve my data, I am using Express and Axios. The code for this looks like: axios.get('http://127.0.0.1:8000/ ...

Is there a glitch in JavaScript when comparing dates?

What's wrong with this code? function test() { var start = new Date(2012, 3, 31, 19, 0, 0); // Start: 3/31/2012 7:00 PM var end = new Date(2012, 4, 1, 1, 0, 0); // End: 4/01/2012 1:00 AM if (end < start) console.log("oops ...

Is there a way to click on an ajax link?

AjaxOptions ajaxMainContent = new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "main_content" }; @Ajax.ActionLink("start new game", "Game", ajaxMainContent) I am looking for a way to trigger this action in javascript. function startNewGame(cost) ...

"Exploring the capabilities of NODE JS Socket IO through request and response communication

I am currently utilizing node js and socket io in my website setup. I am encountering an issue where I need to establish a connection with the client on my website when the "client.on('Connexion', function(data) { }" is triggered. However, I am f ...

Ajax transmitting data with concealed characters

I'm trying to send data from one PHP site to another. On the first site, everything looks good. The string appears as --show="author,book,text/n However, when I check the string after receiving it on the second site, it shows --show="author,b ...

Why is the AJAX POST submission to PHP resulting in an empty POST upon reaching PHP?

I've encountered an issue when submitting a POST request via AJAX to my PHP script. The browser's Network tab confirms that the POST data has values, the $_POST value is true upon reaching the PHP script, and the isset() verification is valid. At ...

PHP code to launch a pop-up on a remote computer

Let me start by explaining the concept of Flow. Imagine UserX is online in country X, and UserY is online in country Y. ---------------- Here's what I want: When UserX clicks on an ajax button, a popup opens on UserY's PC. --- ...