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

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 Jan 5 '14 at 23:14
    
You could just keep it in a $_SESSION[''] varible... that way it's global... everywhere . –  ilarsona Jan 5 '14 at 23:25
    
Where does $id come from in the first place? –  Ja͢ck Jan 5 '14 at 23:26
    
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. –  simeg Jan 5 '14 at 23:27

2 Answers 2

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. –  simeg Jan 5 '14 at 23:32

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. –  simeg Jan 5 '14 at 23:24

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.