finst33
finst33

Reputation: 141

Use php in window.location.href

This is my js code:

function check() {
    var delvar = "<? $_POST["del"]; ?>";
    var answer = confirm("Are you sure you want to delete the article?")
    if (answer) {
        window.location = "adm-site.php?del=" + delvar + "&delete=true";
    }
}

Now, my problem is that the delvar does not work. so the url is just ?del=&delete=true , instead of e.g. ?del=testarticle&delete=true

EDIT: My $_POST["del"] is a select tag with all the articles in it, so you will choose which to delete
The HTML code:

<select name="del">
    <option value="none" SELECTED></option>
    all articles echo'ed here by php
</select>

Upvotes: 0

Views: 22325

Answers (5)

Pradeep
Pradeep

Reputation: 416

var delvar = "<? $_POST["del"]; ?>"; 

You are trying to combine javascript and php but we don't have that type of possibility because javascript is client side scripting language, PHP is server side scripting language. There is another way to combine javascript & php. Please refer this link.

Upvotes: 0

DavChana
DavChana

Reputation: 1976

I believe you want this scenario:

  • User gets a page with a form containing the select tag having all the articles as values/options.
  • User selects an article, which he/she wants to delete.
  • User clicks a button.
  • Then you want to redirect user to URL adm-site.php?del=02&delete=true.

So, in this case, only JavaScript is getting the value of selected article value and embedding it in the redirect URL. So, PHP can do nothing here. All the action is happening client-side.

Try this HTML code:

<html>
<head>
<script language="JavaScript">
    function check(){
        var delvar=document.form1.del.options[document.form1.del.selectedIndex].value;
        //alert(delvar); //debug
        var answer = confirm("Are you sure you want to delete the article?")
        if (answer) {
            //alert("adm-site.php?del=" + delvar + "&delete=true");
            window.location = "adm-site.php?del=" + delvar + "&delete=true";
        }

    }
</script>
</head>
<body>
<form name="form1">
    <select name="del">
        <option value="none" SELECTED>None</option>
        <option value="01">Article 01</option>
        <option value="02">Article 02</option>
        <option value="03">Article 03</option>
    </select>
<input type="button" value="Delete This Article" onclick="check();">
</form>
</body>
</html>

LIVE Demo at JSFiddle.net (Alerting the Redirecting URL)

Things I have edited/changed:

  • Added a name to form tag.
  • Changed the way var delvar is assigned the value of currently selected article.

Else all is same code as yours.

As mentioned by Pheonix, A bit secure code, doing the same thing as the above one, but by using $_POST. You need to use $_POST array instead of $_GET in your adm-site.php.

<html>
<head>
<script language="JavaScript">
    function check(){
        var delvar = document.form1.del.options[document.form1.del.selectedIndex].value;
        var agree = confirm("Are you sure you want to delete Article " + delvar + " ?");
        if (agree)
            return true ;
        else
            return false ;
    } //function check() ENDS
</script>
</head>
<body>
<form name="form1" action="adm-site.php" method="POST">
    <select name="del">
        <option value="none" SELECTED>None</option>
        <option value="01">Article 01</option>
        <option value="02">Article 02</option>
        <option value="03">Article 03</option>
    </select>
    <input type="hidden" name="delete" value="true" />
    <input type="submit" name="submit" value="Delete this Article" onClick="return check();" />
</form>
</body>
</html>

Demo at JSFiddle.net

Upvotes: 2

Alfo
Alfo

Reputation: 4819

Here's another way to think about it: make a link to the page you want to be sent to, and then give it an onclick like this:

<a href="/adm-site.php?del=<?php echo $_POST['del']; ?>&delete=true" onclick="return confirm('Confirm Deletion?')" >Delete</a>

This works, but it might not fit into your environment, depending on what triggers check().

Upvotes: 0

DanMan
DanMan

Reputation: 11561

Your HTML contains " so you need to surround the PHP with single apostrophes. And if you're using the short tag, use it right. So in the end, it should look like this:

var delvar = '<?= $_POST["del"]; ?>';

Upvotes: 0

pcarvalho
pcarvalho

Reputation: 290

var delvar = '<?php echo $_POST["del"]; ?>';

note the <?php instead of the discouraged shortcode too.

Upvotes: 1

Related Questions