0

How can i pass a post array to a functing then pull out the params i.e

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

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



    }

any help pls

2
  • Concretise what isn't working with example two pls? Commented Dec 8, 2011 at 15:46
  • I think you should learn about functions in PHP first Commented Dec 8, 2011 at 15:51

4 Answers 4

4

First you should use $_GET and not $_POST. Second you don't need to call your function parameter as $_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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

For example, if you are using it in a database query then you will need to use mysql_real_escape_string(); and if you are echo'ing the data then use htmlentities();

Comments

0

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.

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!

Comments

0

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. 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. 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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.