0

I am using $_POST to post data to a php file. In that php file, I have the following.

$params = array(
        'name' => "$fname",
        'email' => "$email",
        'ad_tracking' => 'test',
        'ip_address' => '$_SERVER["REMOTE_ADDR"]',
    );
    $subscribers = $list->subscribers;
    $new_subscriber = $subscribers->create($params);

What is the best way to use the $_POST data to define the vales of each keys in the array? The use of $_SERVER["REMOTE_ADDR"] is also not working as hoped.

2 Answers 2

1

POST variables are passed via the super global array $_POST in PHP. So in your case, this would technically work:

$params = array(
    'name' => $_POST['fname'],
    'email' => $_POST['email'],
    'ad_tracking' => 'test',
    'ip_address' => $_SERVER['REMOTE_ADDR'],
);

Your code for $_SERVER["REMOTE_ADDR"] was enclosed in single quotes, which in PHP means a verbatim string (i.e. without variable interpolation).

Btw, you should think of input filtering too - http://www.php.net/filter

To give you an example, this would perform input filtering in your current case:

$filtered = filter_input_array(INPUT_POST, array(
    'fname' => FILTER_SANITIZE_STRING,
    'email' => FILTER_VALIDATE_EMAIL,
);

Each value inside $filtered will either have a value (valid), be NULL (not present) or false (invalid).

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

Comments

0

Regarding "the use of $_SERVER["REMOTE_ADDR"] is also not working as hoped.": Single-Quotes don't evaluate php variables

$params = array(
    'name' => $_POST["fname"],
    'email' => $_POST["email"],
    'ad_tracking' => 'test',
    'ip_address' => $_SERVER["REMOTE_ADDR"],
);
$subscribers = $list->subscribers;
$new_subscriber = $subscribers->create($params);

1 Comment

This is my code: justpaste.it/1gt46. How can i declare more than one id's in variable. Single value declaration is : $productId = 19; How can i declare more than one value in $productId=?

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.