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>
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>
Share
Improve this question
edited Jul 30, 2012 at 11:17
mtk
13.7k16 gold badges75 silver badges116 bronze badges
asked Jul 28, 2012 at 15:00
finst33finst33
1411 gold badge3 silver badges11 bronze badges
7
-
2
What about
echo
ing the variable content? Sample:<?=$_POST["del"]?>
– CodeZombie Commented Jul 28, 2012 at 15:01 -
didnt work with either
echo $_POST["del"]
orecho $del
– finst33 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. – DavChana 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 inwindow.location
, which is in JS (still client side). PHP have no authority here. – DavChana 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./Articles/WellIntentioned-Destruction.aspx – user212218 Commented Jul 28, 2012 at 16:22
5 Answers
Reset to default 2I 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 (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
var delvar = '<?php echo $_POST["del"]; ?>';
note the <?php
instead of the discouraged shortcode too.
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"]; ?>';
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()
.
var delvar = "<? $_POST["del"]; ?>";
You are trying to bine 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 bine javascript & php. Please refer this link.