简体   繁体   English

如何使用ajax和jquery在Google地图的信息窗口内提交表单

[英]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. 如果我删除了使用php post方法提交表单的ajax部分的脚本,则工作正常。 my idea to do it with ajax is that i dont want my marker to go off on submitting the form. 我使用ajax的想法是我不希望我的标记提交表单。 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 这是我的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 请帮助我,它不能正常工作,我不知道thix ajax和jquery代码有什么问题

You'd better have to prevent the form submit event instead of the button click event. 您最好防止表单submit事件,而不是按钮click事件。

So instead of this: 所以代替这个:

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

do this: 做这个:

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

Hope this helps, cheers 希望这会有所帮助,欢呼

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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