0

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!

4
  • 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 ?>'. Commented Jan 5, 2014 at 23:14
  • You could just keep it in a $_SESSION[''] varible... that way it's global... everywhere . Commented Jan 5, 2014 at 23:25
  • Where does $id come from in the first place? Commented Jan 5, 2014 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. Commented Jan 5, 2014 at 23:27

2 Answers 2

1

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)); ?>;
Sign up to request clarification or add additional context in comments.

1 Comment

The alternative answer worked perfectly! Thank you very much, sir.
1

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

No difference, thanks though. Just re-directing me to the page without any $id in the url.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.