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


Pass PHP Variable into Java Script window.location

我正在尝试将 php 变量传递到 java 脚本窗口中。我似乎无法正确语法。

法典:

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

试试这个:

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

你正在将php变量添加到JS变量 var currString = ";

window.location你再次传递PHP变量是错误的,

所以这样做

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

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

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

否则,您会遇到变量包含&等字符的问题。

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