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
echoing the variable content? Sample:<?=$_POST["del"]?>echo $_POST["del"]orecho $delselectelement and you want to bring that value here in this function? SHow your HTML code.selecttag 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 inwindow.location, which is in JS (still client side). PHP have no authority here.