Pass PHP Variable into Java Script window.location

28,622

Solution 1

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");
}

Solution 2

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 &.

Solution 3

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;
Share:
28,622
pixelJockey
Author by

pixelJockey

designer and developer living in Montana USA

Updated on July 09, 2022

Comments

  • pixelJockey
    pixelJockey almost 2 years

    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")
    }
    
  • Rey Gonzales
    Rey Gonzales almost 12 years
    Yaw, the concatenation operator is mixed up. As well as misplacing quotes.
  • pixelJockey
    pixelJockey almost 12 years
    It seems the code fails to open the alert window. My delete button does nothing.
  • pixelJockey
    pixelJockey almost 12 years
    Sorry it does work. It was missing/needed an additional "}". I failed to copy that the first time.
  • pixelJockey
    pixelJockey almost 12 years
    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"); }
  • Parth Thakkar
    Parth Thakkar almost 12 years
    sorry man, no idea! But the request is made right? Maybe some problem with server side. No idea about that right now. :)
  • pixelJockey
    pixelJockey almost 12 years
    Got it: "DELETE FROM resources WHERE parent_category LIKE '%$currString%' AND id = $id LIMIT 1"
  • msEmmaMays
    msEmmaMays over 11 years
    This has typos in the code, you are missing two ; in the echoed javascript
  • msEmmaMays
    msEmmaMays over 11 years
    you are missing a ; after the confirm() and first alert()
  • Parth Thakkar
    Parth Thakkar over 11 years
    ok! corrected!......man! that's an observation! That's real observation! I answered this on 20th June, and you are the first one to find this mistake - 8th August! Awesome!