4

I am trying to pass a php variable into a java script window.location that returns a user to the current list view after deleting an item from the database. I can't seem to get the syntax correct.

Code:

function confirmation(a) {
var currString = "<? echo $currString ?>";
var answer = confirm("Are you sure you want to delete this item?")
if (answer){
 alert("The item has been deleted")
 window.location = "list.php?s='. $currString .'&=delete=true&id=" + a;
}
else{
 alert("The item has not been deleted")
}
asked Jun 20, 2012 at 15:17

4 Answers 4

10

Try this:

function confirmation(a) {
 var currString = "<?php echo $currString ?>";
 var answer = confirm("Are you sure you want to delete this item?");
 if (answer){
 alert("The item has been deleted")
 window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
 }
 else{
 alert("The item has not been deleted");
}
answered Jun 20, 2012 at 15:19
Sign up to request clarification or add additional context in comments.

8 Comments

Yaw, the concatenation operator is mixed up. As well as misplacing quotes.
It seems the code fails to open the alert window. My delete button does nothing.
Sorry it does work. It was missing/needed an additional "}". I failed to copy that the first time.
One last question. Now the alert is working but it doesn't actually delete the entry from the database. This function was working...if(isset($_GET['delete'])) { $id = $_GET['id']; mysql_query("DELETE FROM resources WHERE id = $id LIMIT 1"); }
sorry man, no idea! But the request is made right? Maybe some problem with server side. No idea about that right now. :)
|
1

you are pasing php variable to JS variable var currString = "";

and in window.location you are passing again php variable which is wrong,

so do it like this

window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
answered Jun 20, 2012 at 15:26

Comments

1

The syntax issue was solved by other answers, but you need to take care of an additional issue: URI encoding your variable when you use it in the URL:

window.location = "list.php?s="
 + encodeURIComponent(currString)
 + "&=delete=true&id=" + a;

or else you will run into problems of your variable contains characters like &.

answered Aug 29, 2012 at 7:36

Comments

-3
echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";
Bart
20.1k8 gold badges72 silver badges80 bronze badges
answered Aug 29, 2012 at 7:21

1 Comment

This has typos in the code, you are missing two ; in the echoed javascript

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.