Pass PHP Variable into Java Script window.location

霸气de小男生 提交于 2019-12-04 12:09:55

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

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;

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

sanjay guptya
echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!