Re: Functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



elaborating (?spelling) on Micahs loop:

funcs.php:
<?
function sanitize($glob = null)
{
    if (is_null($glob) || !is_array($$glob)) {
        // set name of the global var we care about
	// if no valid name was passed to the function
	$glob = '_REQUEST';
    }

    // using a variable variable name: $$glob now refers to $_REQUEST
    // assuming no arguments were passed to the function.
    // doing this allows sanitizing just $_POST, $_GET or $_COOKIE
    // in case

    foreach ($$glob as $key => $value) {
    	// sanitize all incomings
	$$glob[$key] = $santizedValue;
    }
}

function validate($value, $type)
{
    $isValid = false;

    switch ($type) {
   	case 'email':
 	    // if email is OK do $isValid = true;
 	    break;
        case 'address':
 	    // check address
 	    break;
        default:
	   // the fallback plan.
	   // e.g.
           if (!empty($value)) {
               $isValid = true;
           }
	   break;
    }

    return $isValid;
}
?>
--endof funcs.php

test.php
<?

require_once ('./funcs.php');

// clean and make safe our incoming values
sanitize('_POST');

$validationFields = array(
    'myname'  => 'name',
    'myemail' => 'email',
    'myaddr'  => 'address',
);

// check to see if something was submitted
if(is_array($_POST) && count($_POST)) {
    define('FORM_SUBMITTED', 1);
}

foreach($validationFields as $field => $type) {
    // possibly we want to valid
    if (defined('FORM_SUBMITTED')) {
        // a form was posted...
        if(!validate($_POST[ $field ], $type)) {
            define('ERROR', 1);
            break; // stop the loop
        }
	$$field = $_POST[ $field ];
    } else {
        $$field = '';
    }	
}

if (defined('FORM_SUBMITTED')) {
    if (!defined('ERROR')) {
        // stick the data in a database or email them...
        define('MSG', 'your data was excellently constructed!');
    } else {
       // something bad happened
       define('MSG', 'something bad happened please check your data');
    }
} else {
    // something bad happened
    define('MSG', 'welcome, please fill in this form');
}

?>
<html>
<body>
<h1>My Form</h1>
<?=MSG;?>
<form method="post" action="<?=$_SERVER['PHPSELF'];?>">
*name: <input name="myemail" value="<?=$myname;?>"/><br />
*email: <input name="myemail" value="<?=$myemail;?>"/><br />
*address: <input name="myemail" value="<?=$myaddr;?>"/><br />
<input type="submit" value="submit" />
</form>
</body>
</html>

--endof test.php

Micah Stevens wrote:

Chis,

I think this might do what you want:

foreach ($_REQUEST as $key => $value) {
switch ($key) {
case "email":
// do email stuff
break;
case "address":
// do address stuff
break;
default:
// in case it's something you haven't planned for.. break;
}
}




On Mon February 2 2004 4:36 pm, Chris Payne wrote:


Hi there everyone,

I need to write a function (New to them but starting to get the hang of it)
that will take the field from a form and then do operations on it (To
remove bad characters, any entered mysql commands etc ....) now that's not
a problem, what I want to do though is write a generic function that will
handle ALL the fields in any form.

For example, if I have 3 input boxes, name, address, email - I would like
the function to recieve the data from each form string and do the operation
and then send the info back keeping the same name and value that it arrived
in.  I'm finding it hard to explain, but basically if a field is called
email and it's value is me@xxxxxx then the function needs to be able to
automatically pickup that the incoming name is email and it's value it
me@xxxxxx then process it and output it again, and i'm confused how to do
it.

I can do it if I specify the name of the form item coming in and going out,
but since I want to make it generic I don't want a zillion different
possible form names in the function :-)

Any help (If you can understand my rambling :-) would be greatly
appreciated.

Regards

Chris



-- PHP Database Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux