/    Sign up×
Community /Pin to ProfileBookmark

extract $_POST not working

I have a “Contact Us” form on a page running locally. I use AJAX to dynamically create and send the query to the PHP script, which “should” extract the variables and send it in the mail. However for some reason when I extract the “$_POST”, all the variables return empty. I am self taught at PHP, so my syntax might not be correct, or it’s possible that I am missing something. Here’s the code:

[code=php]
makeVars();
//sendMail();

function makeVars () {
foreach ($_POST as $key => $val) {
$val = stripslashes($val);
echo “$key: $val <br>”;
}
extract($_POST, EXTR_PREFIX_SAME, “a”);
}
[/code]

That returns a list of variables and their key’s currently, which in a test output as follows:

[code]
request: Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test. Test test test.
ip: ***.0.0.1
firstname: TJ
lastname: ****
email: [email protected]
phone: 123-456-7890
state: AB
regards: Development Services
contact: Email
time: 1187640051718 //To prevent IE caching in AJAX
[/code]

As you can see the above key’s [i]should[/i] be turned into variables, however the following function returns all empty variables.

[code=php]
function sendMail() {
if ($regards == “Standard Mail”) {
$address = “$street Apt or Ste:$aptn $city, $state $zip”;
} else {
$address = $state;
}

$subject = $regards;

$message = “nName: $firstname $lastname n
Title: $jobtitlen
Company: $companyn
Email: $emailn
Phone: $phonen
Address: $addressn
Request: n $requestnnn
Extra Info: n
From: $lastname, $firstnamern
Reply-To: $emailrn
Preferred Contact: $contactn
IP address: $ip n”;

echo nl2br($message);

//mail (“postmaster@****.net”, $subject, $message) or die(“Error sending mail!”);

//echo “Your message has been sent successfully. Thanks for your interest in *****, we look forward to being in contact with you.”;
}[/code]

to post a comment
PHP

12 Comments(s)

Copy linkTweet thisAlerts:
@flannAug 20.2007 — this is how I turn keys into variables.

[code=php]
foreach ($_POST as $n=>$v) {
${$n} = $v;
}
[/code]
Copy linkTweet thisAlerts:
@TJ111authorAug 20.2007 — That's a good idea, I'm going to use that for now. Any advantages/disadvantages over using the extract method?
Copy linkTweet thisAlerts:
@TJ111authorAug 20.2007 — I tried your method yet all the variables are still empty. I can't figure out why. Here's the complete script as it stands.
[code=php]
makeVars();
sendMail();

function makeVars () {
foreach ($_POST as $key => $val) {
${$key} = stripslashes($val);

echo "$$key = ${$key}<br>";

}
}



function sendMail() {
//Make address
if ($contact == "Standard Mail") {
$address = "$street Apt or Ste:$aptn $city, $state $zip";
} else {
$address = $state;
}

//Subject of Message
$subject = $regards;

//Make Message
$message = "nName: ".$firstname." ".$lastname." n";
$message .= "Email: ".$email."n";
$message .= "Phone: ".$phone."n";

if ($jobtitle) {
$message .= "Title: ".$jobtitle."n";
}

if ($company) {
$message .= "Company: ".$company."n";
}

$message .= "Address: ".$address."n";

$message .= "Request: n ".$request."nnn";

$message .= "Extra Info: n";
$message .= "From: ".$lastname.", ".$firstname."rn";

$message .= "Reply-To: ".$email."rn";
$message .= "Preferred Contact: ".$contact."n";
$message .= "IP address: ".$ip." n";

echo $subject ."<br><br>";
echo nl2br($message);

//mail ("*****r@*****.net", $subject, $message) or die("Error sending mail!");

//echo "Your message has been sent successfully. Thanks for your interest in *****, we look forward to being in contact with you.";
}[/code]


And here's what it returns
<i>
</i>$request = Test test test test test.
$ip = ***.0.0.1
$firstname = John
$lastname = Doe
$email = [email protected]
$phone = 123-456-7890
$state = AK
$regards = Development Services
$contact = Email
$time = 1187642893593



Name:
Email:
Phone:
Address:
Request:



Extra Info:
From: ,
Reply-To:
Preferred Contact:
IP address:
Copy linkTweet thisAlerts:
@flannAug 20.2007 — try this:

[code=php]
makeVars($_POST);
sendMail();

function makeVars ($arr) {
foreach ($arr as $key => $val) {
${$key} = stripslashes($val);

echo "$$key = $val<br>";

}
}



function sendMail() {
//Make address
if ($contact == "Standard Mail") {
$address = "$street Apt or Ste:$aptn $city, $state $zip";
} else {
$address = $state;
}

//Subject of Message
$subject = $regards;

//Make Message
$message = "nName: ".$firstname." ".$lastname." n";
$message .= "Email: ".$email."n";
$message .= "Phone: ".$phone."n";

if ($jobtitle) {
$message .= "Title: ".$jobtitle."n";
}

if ($company) {
$message .= "Company: ".$company."n";
}

$message .= "Address: ".$address."n";

$message .= "Request: n ".$request."nnn";

$message .= "Extra Info: n";
$message .= "From: ".$lastname.", ".$firstname."rn";

$message .= "Reply-To: ".$email."rn";
$message .= "Preferred Contact: ".$contact."n";
$message .= "IP address: ".$ip." n";

echo $subject ."<br><br>";
echo nl2br($message);

//mail ("*****[email protected]", $subject, $message) or die("Error sending mail!");

//echo "Your message has been sent successfully. Thanks for your interest in *****, we look forward to being in contact with you.";
}
[/code]


I think it wasn't working because I pretty sure that $_POST isn't global hence you can't use it inside a function.
Copy linkTweet thisAlerts:
@TJ111authorAug 20.2007 — The only way I can make it work is if I remove the function from both of them and use it as an inline script. It seems as if the variables aren't available globally throughout the script. So for now the whole script will be inline, which isn't a big deal, but I would like to eventually get the functions to work.

[code=php]
foreach ($_POST as $key => $val) {
${$key} = stripslashes($val);

echo "$$key = ${$key}<br>";

}





//Make address
if ($contact == "Standard Mail") { //...etc
[/code]
Copy linkTweet thisAlerts:
@flannAug 20.2007 — Did you try my post above where I pass the $_POST array into the function call?
Copy linkTweet thisAlerts:
@TJ111authorAug 20.2007 — Yes I did and it still returned all null values for the variables.
Copy linkTweet thisAlerts:
@NogDogAug 20.2007 — Remember that variables within a function are local to that function, unless they are "super-globals" such as $_SESSION or $_POST, or you explicitly define them as global within the function. So, if extract() is called within a function, the variables it creates will be local to that function.
Copy linkTweet thisAlerts:
@flannAug 20.2007 — I made a correction to it.

[code=php]
makeVars($_POST);
sendMail();

function makeVars ($arr) {
foreach ($arr as $key => $val) {
${$key} = stripslashes($val);

echo "$$key = $val<br>";

}
}
[/code]
Copy linkTweet thisAlerts:
@NogDogAug 20.2007 — PS: you may want to use the [url=http://www.php.net/manual/en/function.import-request-variables.php]import_request_variables[/url]() function, as the variables it creates are in the global scope. Of course, if you do this, you need to be careful about name-space collisions with any other variables in your script (such as by using the optional prefix parameter).
Copy linkTweet thisAlerts:
@TJ111authorAug 20.2007 — I took your advice and declared al the variables as global, however they still return a null value from the sendMail function.

[code=php]
makeVars($_POST);
sendMail();

function makeVars ($arr) {
foreach ($arr as $key => $val) {
global ${$key};

${$key} = stripslashes($val);

echo "$$key = $val<br>";

}
}



function sendMail() {
[/code]
Copy linkTweet thisAlerts:
@NogDogAug 20.2007 — I'd probably make a function that returned an array, and then pull the value from that filtered array:
[code=php]
<?php
function makeVars($data)
{
if(!is_array($data))
{
user_error("makeVars(): supplied arg is not an array");
return(FALSE);
}
array_walk_recursive( // NOTE: requires PHP 5, PHP 4 can only do array_walk()
$data,
create_function(
'&$value,$key',
'$value = stripslashes($value);'
)
);
return($data);
}
// test data
$_POST['test1'] = "This here\'s a test.";
$_POST['test2'] = "It\'s only a test.";
// test usage
$filtered = makeVars($_POST);
printf("<pre>%s</pre>n", print_r($filtered, 1));
[/code]
×

Success!

Help @TJ111 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...