Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass PHP Variable into Java Script window.location

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")
}
like image 558
pixelJockey Avatar asked Jan 17 '23 08:01

pixelJockey


2 Answers

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");
}
like image 153
Parth Thakkar Avatar answered Jan 18 '23 23:01

Parth Thakkar


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;
like image 20
Khurram Avatar answered Jan 18 '23 23:01

Khurram