Unable to invoke the jQuery function within CakePHP3

I am having trouble calling the jQuery function mentioned below in my CakePHP3 MVC project. There are no error messages, but nothing seems to be happening. The code is set up so that jQuery gets included automatically. The function I am trying to call should calculate the latitude and longitude from an address provided in the form elements of the CakePHP3 application.

  <?php
                    echo $this->Form->input('Tutors.address_street');
                    echo $this->Form->input('Tutors.address_suburb');
                    echo $this->Form->input('Tutors.address_postcode');
                    echo $this->Form->input('Tutors.address_state',array('type' => 'select', 'options' => $auStates, 'default' => 'VIC'));
                    ?>
                    <hr>
                    <?php
                    echo $this->Form->input('Tutors.address_lat',["value"=>0]);
                    echo $this->Form->input('Tutors.address_long',["value"=>0]);
                    ?>

                     <input type="button" value="calculate" id="calculate_address_lat_long" onClick="$(this).calculate_address_lat_long();" />

 /////////////   
<script type="text/javascript">
     $(document).on('click', '#calculate_address_lat_long', function () {
        var address = '';
        address += $('#TutorAddressStreet').val();
        address += ' ' + $('#TutorAddressSuburb').val();
        address += ' ' + $('#TutorAddressPostcode').val();
        address += ' ' + $('#TutorAddressState').val();
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                $('#TutorAddressLat').val(latitude);
                $('#TutorAddressLong').val(longitude);
                $('#formated_address_lat_long')
                    .html('<div class="alert alert-success">' + results[0].formatted_address + '</div>')
                    .fadeTo(100, 0.1).fadeTo(250, 1)
                    .css({"position": "relative","top": "15px"});
            } else {
                $('#formated_address_lat_long')
                    .fadeTo(100, 0.1).fadeTo(250, 1)
                    .html('<div class="alert alert-error">22Address Not Found</div>')
                    .css({"position": "relative","top": "15px"});
            }
        });
    });
</script>

Answer №1

Eliminate the onclick function -

<input type="button" value="calculate" id="calculate_address_lat_long" />

or

Consider this alternative

  <?php
                    echo $this->Form->input('Tutors.address_street');
                    echo $this->Form->input('Tutors.address_suburb');
                    echo $this->Form->input('Tutors.address_postcode');
                    echo $this->Form->input('Tutors.address_state',array('type' => 'select', 'options' => $auStates, 'default' => 'VIC'));
                    ?>
                    <hr>
                    <?php
                    echo $this->Form->input('Tutors.address_lat',["value"=>0]);
                    echo $this->Form->input('Tutors.address_long',["value"=>0]);
                    ?>

                     <input type="button" value="calculate" id="calculate_address_lat_long" onClick="calculate_address_lat_long();" />


<script type="text/javascript">
     function calculate_address_lat_long() {
        var address = '';
        address += $('#TutorAddressStreet').val();
        address += ' ' + $('#TutorAddressSuburb').val();
        address += ' ' + $('#TutorAddressPostcode').val();
        address += ' ' + $('#TutorAddressState').val();
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                console.log(results);
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                $('#TutorAddressLat').val(latitude);
                $('#TutorAddressLong').val(longitude);
                $('#formated_address_lat_long')
                    .html('<div class="alert alert-success">' + results[0].formatted_address + '</div>')
                    .fadeTo(100, 0.1).fadeTo(250, 1)
                    .css({"position": "relative","top": "15px"});
            } else {
                $('#formated_address_lat_long')
                    .fadeTo(100, 0.1).fadeTo(250, 1)
                    .html('<div class="alert alert-error">22Address Not Found</div>')
                    .css({"position": "relative","top": "15px"});
            }
        });
    }
</script>

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

Utilizing JQuery to transmit form data to several functions

I'm working on a form with multiple steps that collects data on each step. However, when I submit the form using an ajax request, only the data from the last step is being sent. What's the best approach to solve this issue? <script type="text ...

Setting up an i18n project in AngularJS

I just embarked on my angularjs journey yesterday with little to no prior knowledge about it. My initial goal is to centralize all the labels for my UI in a file (to facilitate swapping them out for i18n). As far as I know, this can be achieved by importi ...

Nothing is being returned when using JQuery AJAX POST

Having trouble identifying the issue, as all that seems to happen is the URL changes to "http://localhost/?search=test" when entering "test", for example. index.php <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquer ...

Monitoring and recording the current line number during evaluation

Recently, I've been experimenting with eval to modify a $scope object named variables. This is the watcher I'm using: $scope.$watch(function () { return $scope.variables; }, function (variables) { console.log('changed!'); }, true) ...

Create automated scripts with Selenium using the programming language JavaScript

Is it possible to create selenium webdriver scripts using only javascript? If so, what are the benefits of choosing javascript over languages like java or C#? In what situations would javascript be the preferred option? Appreciate your insights. ...

"Unusual HTML and jQuery quirk causing a perplexing issue: a function that keeps looping inexp

A unique code written in javascript using jQuery allows users to create a "box" on a website with each click of a button, triggering an alert message upon clicking the box. The process is as follows: 1) Clicking the "Add (#addBox)" button appends a new li ...

Changing the color of tabs using inline styles in material ui does not seem to work

I am brand new to using material ui and have been attempting to alter the colors of the selected tab. Currently, the color is a dark blue shade and I am aiming to change it to red. I tried applying inline styles, but unfortunately, there was no change. C ...

Having trouble with jQuery on my webpage

Looking for assistance with a technical issue I'm facing... I am currently working with AngularJS and have integrated jQuery into my project. However, I've encountered an issue where the jQuery code is not functioning as expected on the HTML pag ...

I have implemented a bootstrap modal, but whenever I trigger a function on the JavaScript side, it automatically closes. I am aiming for the modal to remain open

<div id="responsive-modal3" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none; " data-keyboard="false" data-backdrop="static" ng-init = "populateBidSpocData()" > <div cl ...

Looking for a way to achieve a dynamic image zoom and pan effect similar to Google Maps using jQuery or JavaScript?

The objective is to allow the user to manipulate the image within a static frame and subsequently crop the image using rails. Up until now, my search has led me to , but I am curious if there is a tool that also includes image cropping capabilities? Appr ...

Exploring PHP Queries with Div Classes

I'm in desperate need of assistance. I'm currently working on putting together a website and pulling data from a mysql database. The specific div that I'm trying to populate is provided below. The issue I'm facing is how do I integrate ...

Having trouble interacting with an element using Selenium in Python

I am trying to figure out how to download an image from Instagram, but I cannot seem to programmatically click on the download button of this page. Even after attempting to use XPath and CSS selector methods for clicking, unfortunately, neither seems to w ...

Sending an array as a query string

I am trying to send an array to my server using jsonp. Here is an example of the JSON I want to pass: ["something","another_thing",4,{"iam" : "anobject"}] However, I am unsure about how to actually pass an array. I thought it might work like this: some ...

Change the output of Object.fromEntries

I've been working on updating the values of an object using the .fromEntries() method. The issue I am facing is that even though I am returning a modified Array of hours, when the function completes it reverts back to the original complete Array. If ...

Error encountered when WebDriverIO attempted to connect to Appium due to a lack of compatible capabilities

I'm currently facing an issue while attempting to link my virtual Android Device through Appium with my Webdriverio script for the purpose of automating some testing tasks. Here are the capabilities that I have set: capabilities: [{ // N ...

What are the acceptable ID attribute values to use with JQuery selectors?

Can you tell me what the acceptable values are for the ID attribute in HTML elements when utilizing JQuery selectors? Is JQuery conforming to HTML4 or HTML5 rules regarding the ID attribute? ...

Mobile Chrome allows users to utilize the IFrame player API for enhanced video

Currently, I am working on creating a mobile website where I plan to include some YouTube videos using the IFrame player API (https://developers.google.com/youtube/iframe_api_reference). My main goal is to have the video start playing only after the user ...

Ways to show a corresponding number beneath every image that is generated dynamically

I have a requirement to show a specific image multiple times based on user input. I have achieved this functionality successfully. However, I now need to display a number below each image. For example, if the user enters '4', there should be 4 im ...

Implementing JQuery's load event for image loading

I am looking to dynamically resize the parent container of an image to match the same size as the image itself once it has loaded. Currently, I have been using the following code: $(window).load(function(){ $('.image-principale').each(funct ...

Would it be possible to use a script to convert the y-axis values of the chart into Arabic numerals?

Currently, I am working on creating line and pie charts, but I need the Y-axis to display Arabic language. $('button').on('click', function(e) { $('#pie-chart3').empty(); var type = $(this).d ...