PHP Make a simple if-isset-empty function -
i'm coding worksheet app printer company. i'm getting flood of forms. every single input field have check if $_post variables set, , if, echo value. (in case of error, example after validation error, user shouldn't retype whole form)
sample code:
if(isset($_post['time'])&&!empty($_post['time'])){echo $_post['time'];} i had implement hundred times. tried figure out kind of function make simple , readable.
something this:
function if_post_echo($key, $default = "") { if(isset($_post[$key])&&!empty($_post[$key])){ echo $_post[$key]; }else{ echo $default; } } but wont work. have tried pass in $_post $key variable this:
if_post_echo($_post['time']) function if_request_echo($key, $default = "") { if(isset($key)&&!empty($key)){ echo $key; }else{ echo $default; } } and tried this:
function if_request_echo($key, $default = null) { return isset($_request[$key])&&!empty($_request[$key]) ? $_request[$key] : $default; } without reasonable outcome.
the question:
how can forge function looks necessary $_post variable , returns or if unset returns empty string. , there way $_get , $_request, too? (or duplicate?)
your php testing function:
<?php function test_req($key, $default = '') { if(isset($_request[$key]) , !empty($_request[$key])) { return $_request[$key]; } else { return $default; } } ?> then in form html:
<input name="my_field" value="<?php echo htmlentities(test_req('my_field')); ?>" /> $_request (linked) php super global contains both post ($_post) , ($_get) request parameters.
if want capture post request parameters be:
<?php function test_req($key, $default = '') { if(isset($_post[$key]) , !empty($_post[$key])) { return $_post[$key]; } else { return $default; } } ?> for example.
Comments
Post a Comment