Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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!

share|improve this question
 
This looks od <?php echo "$id" ?>. If $id is a variable remove the double quotes. You also might want to add a query string name for example 'delete_script.php?id=<?php echo $id ?>'. –  synapze 15 hours ago
 
You could just keep it in a $_SESSION[''] varible... that way it's global... everywhere . –  ilarsona 14 hours ago
 
Where does $id come from in the first place? –  Jack 14 hours ago
 
I did try to keep it in a $_SESSION variable, but it wouldn't get into the url anyway. And I'd prefer to do it this way. I have excluded alot of code, the $id is the id of a post in my database. This page gets it from $_GET from another page. –  simpe 14 hours ago
add comment

2 Answers

up vote 1 down vote accepted

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)); ?>;
share|improve this answer
 
The alternative answer worked perfectly! Thank you very much, sir. –  simpe 14 hours ago
add comment

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;
}
share|improve this answer
 
No difference, thanks though. Just re-directing me to the page without any $id in the url. –  simpe 14 hours ago
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.