2011-10-24 38 views
1

我想让我的PHP脚本捕获获取或发布变量。这是我是否改变了我的方法来获取或发布,php脚本应该能够捕获同一个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"]; 
?> 

上面的代码不会捕获后的变量。如何我捕获后的变量?

+3

你为什么不打印'$ name'? – hsz

回答

2

我一直使用的功能我写道:

function getGP($varname) { 
    if (isset($_POST[$varname])) { 
     return $_POST[$varname]; 
    } else { 
     return $_GET[$varname]; 
    } 
} 

然后,只需:

$name = getGP('file'); 
+1

好吧,虽然方便,但这不是最聪明的事情。 –

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']; 
相关问题