简体   繁体   中英

how to submit a form inside the infowindow of google maps using ajax and jquery

below is my code for submitting a form inside the infowindow, but when i click on submit button, the page gets reloaded and nothing happens. if i remove the script of ajax part of submitting the form using php post method, it is working fine. my idea to do it with ajax is that i dont want my marker to go off on submitting the form. i want it right there. below is my code

<script>
    $(document).ready(function()
    {
        $("#mylocation_form").submit(function(e)
        {
            e.preventDefault();
            $("#make_me_live").html("<img src='images/loader3.gif' />submitting...");
            var mylocation=$("#mylocation").val();
            if(mylocation=="")
            {
                $("#make_me_live").show().html("Cannot be empty");
            }
            else
            {
                $.ajax({
                    type:"post",
                    url:"mylocation.php",
                    data:"mylocation="+mylocation,
                       success:function(data){
                        if(data==0)
                        {
                            $("#make_me_live").html("Update not Successfull :(");
                        }
                        else
                        {
                            $("#make_me_live").html("Update successfull :)");
                        }
                    }
                 });
            }
        });
    });
</script>

here is my contentstring for infowindo

var contentstring = '<div id="infowindow_photo">' + '<img src="upload_pic/' + asd + '" height="100%" width="100%"/>' + '</div>'
                    + '<div id="infowindow_name">' + '<p><b>' + fname + ' ' + lname + '</b></p>' + '</div>'
                    + '<form id="mylocation_form" >'
                    + '<input type="text" name="mylocation" id="mylocation" style="height:50px; width:300px; outline:none;"/>'
                    //+ '<br />' + 'upload a photo' + '<input type="file" name="files[]" />'
                    + '<br />' + '<input type="submit" id="infowindow_submit" name="submit" value ="submit"/>'
                    + '</form>'
                    + '<div id="infowindow_menu">' + '</div>';
var infowindow = new google.maps.InfoWindow({
    content: contentstring 

    });

and this is my php code
<?php
    session_start();
    $usname=$_SESSION['username'];
    mysql_connect("localhost","root","********");
    mysql_select_db("anurag");

    $mylocation=$_POST["mylocation"];
    $result1 = mysql_query("SELECT uid FROM members WHERE username='$usname' ");
    $row1=mysql_fetch_array($result1);
    $asd=$row1['uid'];
    $image_name = "temp_map";
    $sql = "INSERT INTO notifications VALUES('$asd','$mylocation',NOW(),'$image_name')";

    if(!mysql_query($sql))
             {
            die('Error :'.mysql_error());
        }
        else
        {
            $find=mysql_num_rows($sql);

                    echo $find;
        }

?>

please help me, its not working fine and i dont know what is the problem with thix ajax and jquery code

You'd better have to prevent the form submit event instead of the button click event.

So instead of this:

$("#infowindow_submit").click(function(e)
{
    e.preventDefault();

do this:

$("#mylocation_form").submit(function(e)
{
    e.preventDefault();

Hope this helps, cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM