scanreg Posted June 29, 2010 Share Posted June 29, 2010 I'm trying to understand the rationale for unsetting things in the following function : function _process_form($method) { if(function_exists("process_form")) { $data = $method; foreach($data as $key=>$val) { if(preg_match("/(submit|required)|(_desc$)/i", $key) == 1) unset($data[$key]); } process_form($data); } } What is the unsetting actually doing? Why do it? I've seen this kind of thing in many form scripts, just trying to figure out why. Thanks Link to comment https://forums.phpfreaks.com/topic/206171-why-unset-stuff-from-form/ Share on other sites More sharing options...
kickstart Posted June 29, 2010 Share Posted June 29, 2010 Hi Appears to loop though an array which is a copy of passed fields (presume the $_GET, $_POST or $_REQUEST arrays), and wipes all those where the key matches a particular pattern. Any that are left are then properly processed. Just appears to be an easy way to mark (ie, delete them rather than flagging them) which fields are not to be process. All the best Keith Link to comment https://forums.phpfreaks.com/topic/206171-why-unset-stuff-from-form/#findComment-1078667 Share on other sites More sharing options...
scanreg Posted June 29, 2010 Author Share Posted June 29, 2010 Hi Appears to loop though an array which is a copy of passed fields (presume the $_GET, $_POST or $_REQUEST arrays), and wipes all those where the key matches a particular pattern. Any that are left are then properly processed. Just appears to be an easy way to mark (ie, delete them rather than flagging them) which fields are not to be process. All the best Keith Thanks Keith Yes, I thought so, but it's in the preg_match that I guess I'm confused. Isn't the preg_match statement matching all those that should be accepted? In other words, if you want to unset all those that don't match, then shouldn't the preg_match be like this (!preg_match - see below): foreach($data as $key=>$val) { if(!preg_match("/(submit|required)|(_desc$)/i", $key) == 1) unset($data[$key]); "If it doesn't match one of these patterns, then unset it" In other words, if it does match the desired pattern, then you do want it, otherwise unset it ?? Thanks Link to comment https://forums.phpfreaks.com/topic/206171-why-unset-stuff-from-form/#findComment-1078704 Share on other sites More sharing options...
kickstart Posted June 29, 2010 Share Posted June 29, 2010 Hi Going to depend how the form / page is designed and so what the field names are. All the best Keith Link to comment https://forums.phpfreaks.com/topic/206171-why-unset-stuff-from-form/#findComment-1078715 Share on other sites More sharing options...
scanreg Posted June 29, 2010 Author Share Posted June 29, 2010 Going to depend how the form / page is designed and so what the field names are. I think I get it now, thanks Link to comment https://forums.phpfreaks.com/topic/206171-why-unset-stuff-from-form/#findComment-1078725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.