将PHP变量传递到Java脚本window.location

将PHP变量传递到Java脚本window.location,php,javascript,window.location,Php,Javascript,Window.location,我试图将一个php变量传递到java脚本window.location中,在从数据库中删除一个项目后,该变量将用户返回到当前列表视图。我似乎不能理解正确的语法 代码: 功能确认(a){ var currString=“”; var answer=confirm(“您确定要删除此项目吗?”) 若有(答复){ 警报(“项目已被删除”) window.location=“list.php?s=”.$currString.&=delete=true&id=“+a; } 否则{ 警报(“项目未被删除”)

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

代码:

功能确认(a){
var currString=“”;
var answer=confirm(“您确定要删除此项目吗?”)
若有(答复){
警报(“项目已被删除”)
window.location=“list.php?s=”.$currString.&=delete=true&id=“+a;
}
否则{
警报(“项目未被删除”)
}
试试这个:

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");
}
功能确认(a){
var currString=“”;
var answer=confirm(“您确定要删除此项目吗?”);
若有(答复){
警报(“项目已被删除”)
window.location=“list.php?s=“+currString+”&=delete=true&id=“+a;
}
否则{
警报(“项目未被删除”);
}

您正在将php变量转换为JS变量 var currString=“”

window.location
中,再次传递错误的php变量

那么就这样做吧

window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
echo“警报('系统信息已保存')”;
echo“window.location=”customer\u detail.php?
客户_id=“.$customer_id.””;

其他答案解决了语法问题,但您需要注意另一个问题:在URL中使用变量时:

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

否则您将遇到问题,因为您的变量包含字符,如
&

偏航、连接运算符混淆以及引号错位。代码似乎无法打开警报窗口。我的删除按钮不起作用。很抱歉,它起作用了。缺少/需要额外的“}”。我第一次复制失败。最后一个问题。现在警报正在工作,但它实际上没有从数据库中删除条目。此函数正在工作…如果(isset($\u GET['delete']){$id=$\u GET['id'];mysql_查询(“从资源中删除,其中id=$id LIMIT 1”);}抱歉,伙计,不知道!但是请求是对的?可能服务器端有问题。现在不知道。:)代码中有输入错误,您缺少两个;在回音javascript中
echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";
window.location = "list.php?s="
                + encodeURIComponent(currString)
                + "&=delete=true&id=" + a;