2011-10-24 64 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']; 
相關問題