Forum Moderators: coopster

Message Too Old, No Replies

Validating Post Variables

Validating Post Variables

         

crankshaft

3:07 am on Apr 2, 2005 (gmt 0)

10+ Year Member



Hi all!

I have the following function which is supposed to strip all whitespaces & replace any occurence of ; with ";" on Post Variables but it does not appear to be working.

Any ideas what could be wrong?

Thanx

PeterC

validate();

function validate()
{
global $HTTP_POST_VARS;
foreach($HTTP_POST_VARS as $key=>$val)
{
$val = trim(str_replace(";", "\";\"",$val));
}
}

dreamcatcher

7:20 am on Apr 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try returning a value back from your function:

function validate()
{
global $HTTP_POST_VARS;
foreach($HTTP_POST_VARS as $key=>$val)
{
$val = trim(str_replace(";", "\";\"",$val));
}
return $val;
}

crankshaft

8:04 am on Apr 2, 2005 (gmt 0)

10+ Year Member



Thanx and Oops :)

One more thing, can I use the fornext to assign array variables to single variables i.e.:

foreach($HTTP_POST_VARS as $key=>$val)
{
$val = trim(str_replace(";", "\";\"",$val));
"$".$key = $val
}

What I want is a series of seperate valiables created in the foreach loop without need to specify them individually like this;

$name = $val['name']
$address = $val['address']

killroy

9:43 am on Apr 2, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



foreach operates on a copy of the array, so assigning anything to this copy won't help.

You want something like this perhaps (Sorry, but I can't workwithout my own indenting):

Oh, and you should use $_POST instead of $HTTP_POST_VARS for forwards compatibility to PHP5, which I use.

function validate(){
global $HTTP_POST_VARS;
$validatedVars=Array();
foreach($HTTP_POST_VARS as $key=>$val){
$validatedVars[$key]=trim(str_replace(";", "\";\"",$val));
}
return $validatedVars;
}

crankshaft

1:10 pm on Apr 2, 2005 (gmt 0)

10+ Year Member



Hi;

Thanx so much for your help, unfortunately it still does not appear to work.

Crankshaft

dcrombie

4:53 pm on Apr 2, 2005 (gmt 0)



foreach($_POST as $key => $val) {  
$_POST[$key] = trim(str_replace(";", "\";\"",$val));
}
extract [php.net]($_POST);