Javascript 将 PHP 变量传递到 Java Script window.location

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11122696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:34:32  来源:igfitidea点击:

Pass PHP Variable into Java Script window.location

phpjavascriptwindow.location

提问by pixelJockey

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.

我试图将一个 php 变量传递到一个 java 脚本 window.location 中,该脚本在从数据库中删除一个项目后将用户返回到当前列表视图。我似乎无法获得正确的语法。

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

回答by Parth Thakkar

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

回答by Jasper de Vries

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

语法问题已由其他答案解决,但您需要注意另一个问题:在 URL 中使用变量时对变量进行URI 编码

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

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

否则你会遇到你的变量包含像&.

回答by Khurram

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

您正在将 php 变量传递给 JS 变量 var currString = "";

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

并且在window.location您再次传递错误的 php 变量中,

so do it like this

所以这样做

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

回答by sanjay guptya

echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";