4

我正在尝试将 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

4 回答 4

10

试试这个:

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");
}
于 2012-06-20T15:19:03.720 回答
1

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

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

所以这样做

window.location = "list.php?s=" + currString + "&=delete=true&id=" + a;
于 2012-06-20T15:26:37.133 回答
1

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

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

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

于 2012-08-29T07:36:52.100 回答
-3
echo "<script>alert('System info has been Save')</script>";
echo "<script>window.location='customer_detail.php?
customer_id=".$customer_id."'</script>";
于 2012-08-29T07:21:53.290 回答