最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Pass PHP Variable into Java Script window.location - Stack Overflow

programmeradmin4浏览0评论

I am trying to pass a php variable into a java script window.location that returns a user to the current list view after deleting an item from the database. I can't seem to get the syntax correct.

Code:

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

I am trying to pass a php variable into a java script window.location that returns a user to the current list view after deleting an item from the database. I can't seem to get the syntax correct.

Code:

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")
}
Share Improve this question asked Jun 20, 2012 at 15:17 pixelJockeypixelJockey 3211 gold badge5 silver badges18 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 10

Try this:

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

you are pasing php variable to JS variable var currString = "";

and in window.location you are passing again php variable which is wrong,

so do it like this

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

The syntax issue was solved by other answers, but you need to take care of an additional issue: URI encoding your variable when you use it in the URL:

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

or else you will run into problems of your variable contains characters like &.

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

评论列表(0)

  1. 暂无评论