admin管理员组文章数量:1389370
I want my php script to capture get or post variables. That is whether I have changed my method to get or post, the php script should be able to capture the variables in the same php variable . How do I achieve this?
HTML Code
<script type="text/javascript">
$(function(){
$("input[type=submit]").click(function(){
//alert($(this).parents("form").serialize());
$.ajax({
type: "get",
url: 'file.php',
data: $(this).parents("form").serialize(),
plete: function(data){
} ,
success:function(data) {
alert(data);
}
});
return false;
})
})
</script>
file.php Code
<?php
$name = $_POST["file"]?$_POST["file"]:$_GET["file"];
echo $_POST["file"];
?>
The above code does not capture post variables .How do I capture post variables?
I want my php script to capture get or post variables. That is whether I have changed my method to get or post, the php script should be able to capture the variables in the same php variable . How do I achieve this?
HTML Code
<script type="text/javascript">
$(function(){
$("input[type=submit]").click(function(){
//alert($(this).parents("form").serialize());
$.ajax({
type: "get",
url: 'file.php',
data: $(this).parents("form").serialize(),
plete: function(data){
} ,
success:function(data) {
alert(data);
}
});
return false;
})
})
</script>
file.php Code
<?php
$name = $_POST["file"]?$_POST["file"]:$_GET["file"];
echo $_POST["file"];
?>
The above code does not capture post variables .How do I capture post variables?
Share Improve this question edited Oct 24, 2011 at 14:47 epascarello 208k20 gold badges205 silver badges244 bronze badges asked Oct 24, 2011 at 14:40 Manish BasdeoManish Basdeo 6,27923 gold badges72 silver badges103 bronze badges 1-
3
Why don't you print
$name
? – hsz Commented Oct 24, 2011 at 14:42
6 Answers
Reset to default 7Use the $_REQUEST
superglobal:
$name = $_REQUEST['file'];
if you would like to filter what is done via a POST
or what is done via a GET
use this:
//for the POST method:
if($_SERVER['REQUEST_METHOD'] === 'POST') {
//here get the variables:
$yourVar = $_POST['yourVar'];
}
//for the GET method:
if($_SERVER['REQUEST_METHOD'] === 'GET') {
//here get the variables:
$yourVar = $_GET['yourVar'];
}
otherwise use the _REQUEST:
$yourVar = $_REQUEST['yourVar'];
I've always used a function I wrote:
function getGP($varname) {
if (isset($_POST[$varname])) {
return $_POST[$varname];
} else {
return $_GET[$varname];
}
}
Then just:
$name = getGP('file');
$_REQUEST
captures both $_GET
and $_POST
variables: http://php/manual/en/reserved.variables.request.php
Use $_REQUEST
http://www.php/manual/en/reserved.variables.request.php
using $_REQUEST
http://php/manual/en/reserved.variables.request.php
本文标签: javascriptIs there a way to capture get or post variables using phpStack Overflow
版权声明:本文标题:javascript - Is there a way to capture get or post variables using php? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744592086a2614552.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论