Javascript 如何使用ajax和jquery在google地图的信息窗口中提交表单
下面是我在infowindow中提交表单的代码,但是当我点击submit按钮时,页面被重新加载,什么也没有发生。如果我删除了使用PHPPost方法提交表单的ajax部分的脚本,它就可以正常工作了。我使用ajax的想法是,我不希望我的标记器在提交表单时出错。我要就在那儿。 下面是我的代码Javascript 如何使用ajax和jquery在google地图的信息窗口中提交表单,javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,下面是我在infowindow中提交表单的代码,但是当我点击submit按钮时,页面被重新加载,什么也没有发生。如果我删除了使用PHPPost方法提交表单的ajax部分的脚本,它就可以正常工作了。我使用ajax的想法是,我不希望我的标记器在提交表单时出错。我要就在那儿。 下面是我的代码 <script> $(document).ready(function() { $("#mylocation_form").submit(function(e)
<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>
$(文档).ready(函数()
{
$(“#mylocation_form”)。提交(功能(e)
{
e、 预防默认值();
$(“#让我活起来”).html(“提交…”);
var mylocation=$(“#mylocation”).val();
如果(mylocation==“”)
{
$(“#让我活起来”).show().html(“不能为空”);
}
其他的
{
$.ajax({
类型:“post”,
url:“mylocation.php”,
数据:“mylocation=“+mylocation,
成功:功能(数据){
如果(数据==0)
{
$(“#让我活起来”).html(“更新未成功:(”);
}
其他的
{
$(“#让我活起来”).html(“更新成功:)”;
}
}
});
}
});
});
这是我的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;
}
?>
var contentstring=''+'''
+“+”“+fname+”+lname+”“+”
+ ''
+ ''
//+“
”+“上传照片”+”
+“
”+”
+ ''
+ '' + '';
var infowindow=new google.maps.infowindow({
内容:contentstring
});
这是我的php代码
您最好阻止表单submit
事件,而不是按钮单击
事件
因此,与此相反:
$("#infowindow_submit").click(function(e)
{
e.preventDefault();
这样做:
$("#mylocation_form").submit(function(e)
{
e.preventDefault();
希望这有帮助,干杯我将为编辑此问题以正确设置代码格式的人鼓掌;)无论是谁做的,我都非常感谢。:)我真的很抱歉,我不知道如何以正确的格式编写它。页面会重新加载,因为javascript代码中有一些错误。要查看该错误,请按F12打开console并查看错误内部。此错误可能会立即出现,并在重新加载后消失。不推荐使用event.returnValue。请改用标准的event.preventDefault()。请看这是提交按钮时出现的错误。我不知道该怎么办。我正在使用event.preventDefault()不,它也不起作用。试了很多次。我只是这样写的,然后检查,但它不工作。因此我将其更改为submit button click事件。