我正在尝试从我的代码转到deleteQns.php
页面,但我似乎无法去那里,因为我总是被重定向回administrator(Quiz2)
。我不知道出了什么问题。我希望你们都能帮助我。
这是我的代码:
<?php while($row=$ result->fetch_array(MYSQLI_ASSOC)) { ?>
<tr class="size">
<form id="button" action="administrator(Quiz2).php" method="post">
<th>
<p>No.
<?=$row[ "questionid"]?>
</p>
</th>
<th>
<?=$row[ "questiontext"]?>
</th>
<th class="editanddelete">
<input type="hidden" name="qid" value="<?=$row[" questionid "]?>">
<input type="submit" value="Edit" name="editbtn">
</form>
<form id="delete" action="deleteQns.php" method="post">
<button onclick="myFunction()">Delete</button>
<input type="hidden" name="qid" value="<?=$row[" questionid "]?>">
<p id="demo"></p>
<script>
function myFunction() {
var x;
if (confirm("Are You Sure You Want To Delete This Question?") == true) {
window.location = "deleteQns.php";
} else {
x = "";
}
document.getElementById("demo").innerHTML = x;
}
</script>
</form>
<?php } ?>
答案 0 :(得分:1)
此处出现问题:
window.location = "deleteQns.php";
您无法重定向,因为window.location
是具有多个功能的对象,用于处理页面的Web协议。所以当你想要改变它时,最好的方法就是这样做
window.location.href = window.location.hostname+"/deleteQns.php";
例如,这等于:http://google.com/deleteQns.php
您可以详细了解window.location
:MDN
如何检查代码是否无效的另一种方法是console。
Windows: CTRL - SHIFT - J
Mac:⌥ - ⌘ - J
也可通过扳手菜单(工具&gt; JavaScript控制台)获取:
(通过Runscope API工具回答)
在这里你可以这样调试:
<button onclick="myFunction()">Delete</button>
<input type="hidden" name="qid" value="<?=$row[" questionid "]?>">
<p id="demo"></p>
<script>
function myFunction() {
var x;
var confirmed = confirm("Are You Sure You Want To Delete This Question?");
/* For logging data to console */
console.log(confirmed);
if (confirmed == true) {
window.location.href = window.location.hostname+"/deleteQns.php";
} else {
x = "";
}
document.getElementById("demo").innerHTML = x;
}
</script>