javascript 在 window.location.href 中使用 php
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11702095/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use php in window.location.href
提问by finst33
This is my js code:
这是我的js代码:
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
现在,我的问题是delvar
不起作用。所以网址只是?del=&delete=true
,而不是例如?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:
编辑:我$_POST["del"]
是一个选择标签,其中包含所有文章,因此您将选择要删除
的内容 HTML 代码:
<select name="del">
<option value="none" SELECTED></option>
all articles echo'ed here by php
</select>
回答by DavChana
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
.
- 用户获得一个页面,其中包含一个包含所有文章作为值/选项的选择标签的表单。
- 用户选择他/她想要删除的文章。
- 用户单击按钮。
- 然后您想将用户重定向到 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.
因此,在这种情况下,只有 JavaScript 会获取所选文章值的值并将其嵌入到重定向 URL 中。所以,PHP 在这里什么也做不了。所有的动作都发生在客户端。
Try this HTML code:
试试这个 HTML 代码:
<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)
JSFiddle.net 上的现场演示(警告重定向 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.
- 添加了一个名称来
form
标记。 - 更改了
delvar
为当前所选文章分配值的方式。
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
.
正如Pheonix所提到的,一个有点安全的代码,与上面的代码做同样的事情,但使用 $_POST。您需要使用$_POST
数组而不是$_GET
在您的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>
回答by pcarvalho
var delvar = '<?php echo $_POST["del"]; ?>';
note the <?php
instead of the discouraged shortcode too.
请注意<?php
而不是不鼓励的短代码。
回答by DanMan
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:
您的 HTML 包含,"
因此您需要用单个撇号将 PHP 括起来。如果您使用的是短标签,请正确使用。所以最后,它应该是这样的:
var delvar = '<?= $_POST["del"]; ?>';
回答by Alfo
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:
这是另一种思考方式:创建一个指向您要发送到的页面的链接,然后给它一个onclick
这样的链接:
<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()
.
这有效,但它可能不适合您的环境,具体取决于触发check()
.
回答by Pradeep
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.
您正在尝试结合 javascript 和 php,但我们没有这种可能性,因为 javascript 是客户端脚本语言,PHP 是服务器端脚本语言。还有另一种结合javascript和php的方法。请参考此链接。