I have a question that will probably be easy for someone to answer.

I have a form with about 20 fields +- to be entered...

When submitted, I check for the validity and existance of a few of the fields that are important, but not all of them

What I would like to do is to sort of clean up every field using a function that I use on some other individual field input forms.

My question is, how would I itrate through all of the $POST data and send each through the function, without calling each one by name?

Here is the function, although I don't know that it is relavent to the question:

function cleanString($string){
    htmlentities(mysql_real_escape_string($string));
    return $string;
}//end function

I've used that function for so long that I've forgotten what exactly it does... I think it removes unnecessary white space and protects against malicious data entry... at least that is what I hope it does.

Thanks in advance for your feedback

Douglas

Recommended Answers

All 2 Replies

Member Avatar for diafol

For a simple cleaning measure like mysql_real_escape_string() for everything, you can use array_map():

$cleanpost = array_map('mysql_real_escape_string',$_POST);

However, this doesn't validate your data (obviously). So all your POST vars are now in the $cleanpost array, e.g. $_POST['surname'] will be cleaned to $cleanpost['surname'].

If you need something more 'custom' you can apply user functions:

$cleanpost = array_map('myCleaner',$_POST);
function myCleaner($string){
    return htmlentities(mysql_real_escape_string($string));
}

Thank You ardav... that sounds like exactly what I was looking for...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.