phpjqueryajaxgoogle-mapsinfowindow

Submitting a form with ajax within infowindow GoogleMaps API


Development newbie here. I'm trying to have a form inside each of my infowindows on a google map on my website. I have one function that generates all of the markers as well as the content of each of the markers.

My problem is that the jQuery that should be called after a form in my infowindow is submitted never called (at least addComment.php is never called). I've looked around a lot and couldn't find anything to fix this problem. Any help would be very appreciated

var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 15,
    center: new google.maps.LatLng(38.64806723893503, -90.30880584275044),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var pdata;
$.ajax({type:'POST', url: 'fetchInfo.php', data: pdata, dataType: 'json', success: function(response) {

    var infowindow = new google.maps.InfoWindow();

    var marker, i;

    var content = new Array();

    for (i = 0; i < response.length; i++) {  

        content[i] = '<div> '+ response[i].added;

        content[i] += '<div class=description>'+response[i].desc+'</div>';
        content[i] += '</div>';
        content[i] += '<div class=addCom>';
        content[i] += '<textarea rows="4" cols="10" name="comment" id="comment" data-picId='+response[i].picture_id+' placeholder="Enter Comment Here..."></textarea><br>';
        content[i] += '<input class="submitComment" type="button" value="Add Comment"/>';
        content[i] += '</div>';

        marker = new google.maps.Marker({
            position: new google.maps.LatLng(response[i].lat, response[i].lng),
            map: map
        });

        google.maps.event.addListener(marker, 'click', (function(marker, i) {
            return function() {
                map.panTo(marker.position);
                infowindow.setContent(content[i]);
                infowindow.open(map, marker);
            }
        })(marker, i));
    }
}});

Then, separate from this function I have a jQuery that performs the ajax call

 $(".submitComment").click( function(){
    var comment = $("#comment").val();
    var picture_id = $(this).attr('data-picId');
    var user_id = usrId;
    if (comment === ""){
        return;
    }
    var pdata = {
        comment : comment,
        picture_id : picture_id,
        user_id : user_id
    };
    $.ajax({type:'POST', url: 'addComment.php', data: pdata, dataType: 'json', success: function(response) {
        if(response.success){ 
            $("#uploadfile_input").val("");
            $("#lat").val("");
            $("#lng").val("");
            $("#desc").val("");
            load();
        }
    }
    });
    });

Solution

  • By looking at your script , there are some points of facts.

    -

    First of all attribute 'id' is meant to be uniqueness so you must assign unique ids to comments. content[i] += '<textarea rows="4" cols="10" name="comment" id="comment" data-picId='+response[i].picture_id+' placeholder="Enter Comment Here..."></textarea><br>';

    for(){
                content[i] = '<form><div> '+ response[i].added;
        
                content[i] += '<div class=description>'+response[i].desc+'</div>';
                content[i] += '</div>';
                content[i] += '<div class=addCom>';
                content[i] += '<textarea rows="4" cols="10" name="comment" id="comment" data-picId='+response[i].picture_id+' placeholder="Enter Comment Here..."></textarea><br>';
                content[i] += '<input class="submitComment" type="button" value="Add Comment"/>';
                content[i] += '</div></form>';
       
    
        google.maps.event.addListener(marker, 'click', (function(marker, i) {
                        return function() {
                            map.panTo(marker.position);
                            infowindow.setContent(content[i]);
                            infowindow.open(map, marker);
                        }
                    })(marker, i)); 
            $(content[i]).submit(listenformSubmission());
            }
                function listenformSubmission(){
                    var comment = $(this).find('[name="comment"]');//here will be the form object
                    var picture_id = comment.attr('data-picId');
                    var user_id = usrId;
                    if (comment === ""){
                        return;
                    }
                    var pdata = {
                        comment : comment,
                        picture_id : picture_id,
                        user_id : user_id
                    };
                    $.ajax({type:'POST', url: 'addComment.php', data: pdata, dataType: 'json', success: function(response) {
                        if(response.success){ 
                            $("#uploadfile_input").val("");
                            $("#lat").val("");
                            $("#lng").val("");
                            $("#desc").val("");
                            load();
                        }
                    }
            return false;
                    }
    Here is the more detail with sample code I am not removing the above answer as explanation to this answer. Here is a code which might assist you in this case. First of all I wrote a click listener to be available in the memory

     function addCommentForm(el){
     var formdata = $(el);
       var siblings = formdata.siblings();
        var comments= $(siblings[1]).val();
           console.log(comments);
            if (comments === "") {
                return;
            }
            var pdata = {
                comment:comments
    
            };
        console.log(pdata);
            $.ajax({
    
                type: 'POST', url: 'addComment.php', data: pdata, dataType: 'json', success: function (response) {
    
                   alert((JSON.stringify(response)));
                    }
    
            });
        return false;
    
    }
    

    and here is the init Function where the story starts

     function initMap() {
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 15,
            center: new google.maps.LatLng(38.64806723893503, -90.30880584275044),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });
    
        //  alert("ok");
        $(document).ready(function () {
            var pdata;
            $.ajax(
                    {
                        type: 'POST',
                        url: 'fetchInfo.php',
                        data: {},
                        dataType: 'json',
                        success: function (response) {
    
                            var infowindow = new google.maps.InfoWindow();
                            var marker, i;
                            console.log(response);
                            var content = [];
                            for (i = 0; i < response.length; i++) {
                                content[i] = '<form ><div> ' + response[i].added;
    
                                content[i] += '</div>';
                                content[i] += '<div class=addCom>';
                                content[i] += '<div class=description>' + response[i].desc + '</div>';
                                content[i] += '<textarea rows="4" cols="10" name="comment" id="comment" data-picId=' + response[i].picture_id + ' placeholder="Enter Comment Here..."></textarea><br>';
                                content[i] += '<input onclick="return addCommentForm(this);" class="submitComment" type="button" value="Add Comment"/>';
                                content[i] += '</div></form>';
                                marker = new google.maps.Marker({
                                    position: new google.maps.LatLng(response[i].lat, response[i].lng),
                                    map: map
                                })
                                google.maps.event.addListener(marker, 'click', (function (marker, i) {
                                    return function () {
                                        console.log($(content[i]));
                                        map.panTo(marker.position);
                                                                           infowindow.setContent(content[i]);
    
    
                                        infowindow.open(map, marker);
                                    }
                                })(marker, i));
    
                            }
                        }
                    });
        });
    }
    

    I wrote some PHP SCRIPTS to check the response and here it is fetchInfo.php

        <?php
    echo json_encode(array(array(
        'lat'=>"38.64806723893503",
        'lng'=>"-90.30880584275044",
        'added'=>"1",
        'desc'=>"Descripion of map",
        'picture_id'=>1
    )));
    

    and addComment.php to check the response is recieved

        <?php
          echo json_encode(
        array('result'=>true,
        'submitdata'=>$_POST
        )
        );