简体   繁体   English

将帖子数组传递给函数

[英]passing post array to function

How can i pass a post array to a functing then pull out the params ie 我怎样才能将一个发布数组传递给一个函数,然后拉出参数,即

so i am posting the following. 所以我发布以下内容。

url=http%3A%2F%2Fwww.facebook.com%2Ftest&userId=680410999&location=Cardiff&lang=en_US&social=facebook

can i grab it like this from the function???? 我可以从功能中像这样抓住它吗?

function login($_POST){

//then output the var her.

$_POST['url']; etc

}

this is to save me doing the following in my ajax.php file 这是为了节省我在ajax.php文件中执行以下操作的过程

function login($_POST['url'],$_POST['userId'],$_POST['location'],$_POST['lang']){



    }

any help pls 任何帮助

First you should use $_GET and not $_POST . 首先,您应该使用$_GET而不是$_POST Second you don't need to call your function parameter as $_GET . 其次,您不需要将函数参数称为$_GET You can do this: 你可以这样做:

function login($array)
{
   // do stuff with array
}

and then call the function in this way: 然后以这种方式调用该函数:

login($_GET);

An additional suggest is to read the following quote and that is the difference of actual parameters towards formal parameters : 另一个建议是阅读以下引用,这是实际参数形式参数的区别:

Formal parameters are the parameters as they are known in the function definition. 形式参数是函数定义中已知的参数。 Actual parameters (also known as arguments) are what are passed by the caller. 实际参数(也称为参数)是调用方传递的参数。

If I unserstand you correctly you want a function that you call and pass it an array (in your case the $_POST or $_GET array) as a parameter. 如果我对您的理解不正确,则需要一个函数,然后将其作为参数传递给数组(在您的情况下为$_POST$_GET数组)。

So your function would look something like this: 因此,您的函数将如下所示:

// not sure what you're doing with your login options but here's an example of getting them out of the array and putting them in variables - using the url as an example here...
var url;

function login($loginOptions) {

    // set the url from the array
    url = $loginOptions["url"];

    // do the same for all your other variable...
}

Then you would call your function like so: 然后,您将这样调用函数:

login($_POST); // or $_login($_GET); depending on what you're using

And that's it! 就是这样!

The other answers you have got should solve this problem for you. 您得到的其他答案应该可以为您解决此问题。

Just some more advice, remember to clean any $_POST and $_GET data that you are using in your code. 仅提供一些建议,请记住清除代码中使用的所有$_POST$_GET数据。

For example, if you are using it in a database query then you will need to use mysql_real_escape_string(); 例如,如果在数据库查询中使用它,则将需要使用mysql_real_escape_string(); and if you are echo'ing the data then use htmlentities(); 如果回显数据,则使用htmlentities();

I think what you mean is parsing array to a function for this I will tell you to use a function and connected it with SESSION as this below. 我认为您的意思是为此将数组解析为函数,我将告诉您使用函数并将其与SESSION连接,如下所示。 You can use the $_GET[$var] to retrieve the variable but it's quite hard to be handled by the method get, you can use the method $_POST and combine it with session to save the data , I will explain how to collaborate the session each page so that you can easily retrieve the variable you wanted. 您可以使用$ _GET [$ var]来检索变量,但是get方法很难处理,可以使用$ _POST方法并将其与会话结合起来以保存数据,我将说明如何协作对每个页面进行会话,以便您可以轻松检索所需的变量。 Here's the code : 这是代码:

<?php
    session_start();
    if(isset ($_POST['postedit']))
    {
         $_SESSION['url'] = $_POST['urlsend'];
         header("location:dua.php");
    }
?>
<html>
    <body>
      <form action="satu.php" method="POST">
            Name : <input type="text" name="urlsend"> <br> <br>
            <input type="submit" name="postedit" value="SEND DATA">
      </form>  
    </body>
</html>

<?php
session_start();
if(isset ($_SESSION['url']))
{
    //if data empty then will be redirected to the previous page
   echo $_SESSION['url'];
}
?>

I created with dynamic data even you can choose to use this method or other method. 我创建的都是动态数据,即使您可以选择使用此方法还是其他方法。 I hope you can understand. 我希望你能够明白。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM