在谷歌地圖infowindow下面是我的代碼在infowindow內提交表單,但是當我點擊提交按鈕時,頁面重新加載並且什麼也沒有發生。如果我刪除使用php post方法提交表單的ajax部分的腳本,它工作正常。我的想法是使用ajax來完成,我不希望我的標記在提交表單時失效。我想在那裏。下面 是我的代碼如何提交表單使用ajax和jquery
<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;
}
?>
請幫幫我,它不工作很好,我不知道什麼是觸變Ajax和jQuery代碼
我拍誰編輯這個問題的正確格式化代碼的一個; ) –
非常感謝任何人做過。 :)我真的sry我dnt知道如何寫在一個正確的格式 – user3180305
頁面重新加載,因爲你的JavaScript代碼中有一些錯誤。要查看該錯誤,請按F12打開控制檯並查看錯誤內部。此錯誤可能會立即出現,並在重新加載後消失。 –