I am trying to create a Delete-button with a user confirmation-pop-up.
HTML/PHP:
<form method='POST' onclick='return confirmDelete()'>
<button type='submit'>
Delete script
</button>
</form>
JavaScript:
<script type="text/javascript">
function confirmDelete() {
if (confirm("Are you sure?")) {
window.location.href = 'delete_script.php?<?php echo "$id" ?>';
return false;
}
else{
return true;
}
}
</script>
What it does is that it re-directs me to the correct page, "delete_script.php" but the $id does not appear in the url. The "delete_script.php"-page works with other pages, but I don't know what's wrong with this code. I really think it's possible. Any help would be appreciated!
asked Jan 5, 2014 at 23:11
simeg
1,8792 gold badges27 silver badges34 bronze badges
2 Answers 2
You're expected to pass a url-encoded value for the query-string:
<?php echo 'id=' . urlencode($id); ?>
Alternatively, generate the whole url in php:
location.href = <?php echo json_encode('delete_script.php?id=' . urlencode($id)); ?>;
answered Jan 5, 2014 at 23:24
Ja͢ck
174k39 gold badges269 silver badges317 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
simeg
The alternative answer worked perfectly! Thank you very much, sir.
As per my comment, change this...
if (confirm("Are you sure?")) {
window.location.href = 'delete_script.php?<?php echo "$id" ?>';
return false;
}
to this..
if (confirm("Are you sure?")) {
window.location.href = 'delete_script.php?id=<?php echo $id ?>';
return false;
}
1 Comment
simeg
No difference, thanks though. Just re-directing me to the page without any $id in the url.
default
$idcome from in the first place?