2012-06-20 46 views
2

我想将一个php变量传递给一个java脚本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") 
} 

回答

8

试试这个:

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

偏航,连接运算符混在一起。以及错位报价。 –

+0

看来代码无法打开警报窗口。我的删除按钮什么都不做。 – pixelJockey

+0

它似乎在这里工作:http://jsfiddle.net/sgEWz/ –

1

你考取PHP变量到JS变量VAR currString = “”;

window.location你逝去再次PHP变量,它是错的,

所以做这样的

window.location = "list.php?s=" + currString + "&=delete=true&id=" + a; 
-3
echo "<script>alert('System info has been Save')</script>"; 
echo "<script>window.location='customer_detail.php? 
customer_id=".$customer_id."'</script>"; 
+0

这在代码中有错别字,你错过了两个;在回显的JavaScript中 – RobertMaysJr

1

的语法问题是由其他的答案解决,但是你需要采取照顾一个额外的问题:URI encoding当你在URL中使用它时你的变量:

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

否则你会遇到你的变量的问题包含像&这样的字符。