I'm trying to go to the deleteQns.php
page from my code, but I can't seem to go there as I'm always redirected back to administrator(Quiz2)
. I don't know what's wrong. I hope you all will be able to help me.
Here is my code:
<?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 } ?>
Problem occurs here:
window.location = "deleteQns.php";
You cannot be redirected because window.location
is object with several functions for working with web protocol of the page. So when you want to change it, the best way how to do it
window.location.href = window.location.hostname+"/deleteQns.php";
For example this is equal to : http://google.com/deleteQns.php
You can read more about window.location
: MDN
Another way how to check if your code doesn't work is console.
Windows: CTRL-SHIFT-J
Mac: ⌥-⌘-J
Also available through the wrench menu (Tools > JavaScript Console):
(Answer by Runscope API Tools)
And here you can debug like this:
<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>