0

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>
7
  • 2
    What about echoing the variable content? Sample: <?=$_POST["del"]?> Commented Jul 28, 2012 at 15:01
  • didnt work with either echo $_POST["del"] or echo $del Commented Jul 28, 2012 at 15:04
  • Ok, so you mean there is a form which contains select element and you want to bring that value here in this function? SHow your HTML code. Commented Jul 28, 2012 at 15:05
  • 1
    I think OP wants to insert the value of select tag in the redirect URL, so that there he can access it in $_GET array. So, PHP is not gonna work here, because Form is at client-side, user selects/changes the value, and then OP wants this value to embed in window.location, which is in JS (still client side). PHP have no authority here. Commented Jul 28, 2012 at 15:46
  • 1
    You might want to consider using a POST to submit the deletion request instead of a GET request. See thedailywtf.com/Articles/WellIntentioned-Destruction.aspx Commented Jul 28, 2012 at 16:22

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

Comments

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

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

2 Comments

Where does the official PHP documentation say that the short_open_tag directive is deprecated?
changed wording to reflect your comment
0

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"]; ?>';

1 Comment

I love it when the cowards vote you down and don't even explain themselves.
0

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().

1 Comment

On the same page, where select is selected, and JavaScript code is executed, PHP can not echo the $_POST['del'] on Client Side.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.