我有一個包含多個表單的頁面,這些表單刪除了MySQL數據庫中的行。表單也從MySQL數據庫填充。使用ajax提交表單數據而不刷新
我試圖sumbit沒有刷新頁面的形式,但行不會從我的數據庫中刪除。
更新: 我最初沒有提到包含我動態生成的表單的div。
該表單基於選擇列表中的選項。代碼:
<form>
<h3>Vælg medlem</h3>
<select name="users" onchange="showUser(this.value)">
<?php
$i = 0;
while($row = mysql_fetch_array($result))
{ if (!$i++) echo "<option selected='selected'>Vælg medlem</option>" ?>
<option value="<?php echo $row[medlemmer_id]; ?>"><?php echo $row[medlemmer_navn]; ?></option>
<?php } $i++; ?>
</select>
</form>
表單使用JS將表單添加到頁面上的div。代碼:
<script>
function showUser(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
};
</script>
更新結束
形式:
$(function() {
$('#myform2').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'delete.php',
data: $("#relation_id").val();,
success: function() {
alert('form was submitted');
}
});
});
});
刪除行PHP文件:
<form id="myform2">
<p><?php echo $row[boder_navn]; ?> <?php echo $row[boder_pris]; ?> kr. - <?php echo $dato; ?></p>
<input type="hidden" id="relation_id" name="relation_id" value="<? echo $row[relation_id]; ?>"/>
<input name="submit" type="submit"/>
</form>
發送數據的腳本
<?php
mysql_connect("#######", "########", "#######") or die(mysql_error());
mysql_select_db("#######") or die(mysql_error());
mysql_query("DELETE FROM boder_has_medlemmer WHERE relation_id = ". $_POST['relation_id'] ."");
?>
如果我只是使用method="POST"
和action="delete.php
的表格,它的工作。這意味着有問題的行被刪除,但頁面刷新。
任何建議,我要去哪裏錯了?
你中得到任何錯誤安慰 ? – Khushboo 2014-09-12 11:42:11
如果控制檯沒有顯示任何內容 - 請查看php錯誤日誌。添加一個錯誤輸出到你的delete.php,只是爲了看ajax調用是否真的激發了php。也許這是一個簡單的路徑問題... – errand 2014-09-12 11:44:41
控制檯沒有顯示任何錯誤。我將如何應用錯誤日誌到PHP腳本? – 2014-09-12 12:03:57