繁体   English   中英

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

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

下面是我在信息窗口中提交表单的代码,但是当我单击“提交”按钮时,页面被重新加载并且什么也没有发生。 如果我删除了使用php post方法提交表单的ajax部分的脚本,则工作正常。 我使用ajax的想法是我不希望我的标记提交表单。 我要在那里。 下面是我的代码

<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>

这是我的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;
        }

?>

请帮助我,它不能正常工作,我不知道thix ajax和jquery代码有什么问题

您最好防止表单submit事件,而不是按钮click事件。

所以代替这个:

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

做这个:

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

希望这会有所帮助,欢呼

暂无
暂无

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

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