将PHP变量传递给Javascript window.location

时间:2012-06-20 15:17:22

标签: php javascript window.location

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

代码:

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

4 个答案:

答案 0 :(得分:9)

试试这个:

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

答案 1 :(得分:1)

你将php变量传递给JS变量var currString =“”;

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

所以这样做

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

答案 2 :(得分:1)

语法问题已通过其他答案解决,但您需要处理其他问题:URI encoding您在网址中使用变量时的变量:

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

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

答案 3 :(得分:-3)

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