我希望我的php脚本捕获get或post变量。这就是我是否已经改变了我的方法来获取或发布,php脚本应该能够捕获相同php变量中的变量。 我如何实现这一目标?
HTML代码
<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(),
complete: function(data){
} ,
success:function(data) {
alert(data);
}
});
return false;
})
})
</script>
file.php代码
<?php
$name = $_POST["file"]?$_POST["file"]:$_GET["file"];
echo $_POST["file"];
?>
以上代码不捕获帖子变量。如何捕获帖子变量?
答案 0 :(得分:7)
使用$_REQUEST
超全球:
$name = $_REQUEST['file'];
答案 1 :(得分:2)
我一直使用我写的函数:
function getGP($varname) {
if (isset($_POST[$varname])) {
return $_POST[$varname];
} else {
return $_GET[$varname];
}
}
然后只是:
$name = getGP('file');
答案 2 :(得分:2)
如果您想通过POST
过滤所做的工作或通过GET
完成的工作,请使用此功能:
//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'];
}
否则使用_REQUEST:
$yourVar = $_REQUEST['yourVar'];
答案 3 :(得分:1)
$_REQUEST
捕获$_GET
和$_POST
个变量:http://php.net/manual/en/reserved.variables.request.php
答案 4 :(得分:1)
答案 5 :(得分:1)