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
Add a ment  | 

6 Answers 6

Reset to default 7

Use 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