PHP Make a simple if-isset-empty function
Solution 1
Your PHP testing function:
<?php
function test_req($key, $default = '') {
if(isset($_REQUEST[$key]) and
!empty($_REQUEST[$key])) {
return $_REQUEST[$key];
} else {
return $default;
}
}
?>
Then in your form HTML:
<input name="my_field" value="<?php echo htmlentities(test_req('my_field')); ?>" />
$_REQUEST
(linked) is a PHP super global that contains both POST ($_POST
) and GET ($_GET
) request parameters.
If you only want to capture POST request parameters then it would be:
<?php
function test_req($key, $default = '') {
if(isset($_POST[$key]) and
!empty($_POST[$key])) {
return $_POST[$key];
} else {
return $default;
}
}
?>
For example.
Solution 2
If you have a large amount of fields, I would propose that you also use an array of defaults:
$defaults = array(
"time" => "default",
"name" => "enter name here",
"text..." => "...",
);
$fields = array_filter($_POST) + $defaults;
$fields
will then contain a list of form values with either the POST data or a preset default. No isset, see?
array_filter man page particularly: If no callback is supplied, all entries of input equal to FALSE will be removed. Goes some way to explaining the working behind this solution.
Solution 3
This should work:
function if_post_echo($key, $default = ''){
if(isset($_POST[$key]) AND !empty($_POST[$key]){
echo $_POST[$key];
}
echo $default;
}
If you're having problems I recommend that you try var_dump($_POST)
or print_r($_POST)
to see if everything has been properly posted.
Solution 4
Just to note, this is redundant:
isset($_POST[$key]) && !empty($_POST[$key])
An unset variable is going to always be "empty", so isset()
is implied in your empty()
call.
For your logic you can achieve the same result with just:
!empty($_POST[$key])
Solution 5
Your first function works perfectly to me.
Why do you think it doesn't work?
However, a better variant would be
function _post($key, $default = "") {
if(isset($_POST[$key])){
return $_POST[$key];
}else{
return $default;
}
}
To use it :
echo $_post($key); // You could define the message as a second parameter.
Kael
Updated on July 07, 2022Comments
-
Kael almost 2 years
I'm coding a worksheet app for a printer company. I'm getting flood of forms. For every single input field I have to check if the
$_POST
variables are set, and if, so echo back the value. (In case of some error, for example after a validation error, the user shouldn't retype the whole form)Sample code:
if(isset($_POST['time'])&&!empty($_POST['time'])){echo $_POST['time'];}
I had to implement this about a hundred times. So I tried to figure out some kind of function to make this simple and readable.
Something like this:
function if_post_echo($key, $default = "") { if(isset($_POST[$key])&&!empty($_POST[$key])){ echo $_POST[$key]; }else{ echo $default; } }
But this wont work. I have tried to pass in the
$_POST
for the$key
variable like this:if_post_echo($_POST['time']) function if_request_echo($key, $default = "") { if(isset($key)&&!empty($key)){ echo $key; }else{ echo $default; } }
And I also tried this:
function if_request_echo($key, $default = null) { return isset($_REQUEST[$key])&&!empty($_REQUEST[$key]) ? $_REQUEST[$key] : $default; }
Without any reasonable outcome.
The question:
How can I forge a function that looks for the necessary
$_POST
variable and returns it or if its unset then returns an empty string. And is there a way to do this for$_GET
and$_REQUEST
, too? (Or simply duplicate?) -
Treffynnon about 13 yearsI like the simplicity of this approach.
-
mario about 13 yearsThanks; also for adding the link! (It indeed works similar to
empty()
in this case.) -
Kael about 13 yearsI'm really messed up. With exactly this code you used i paste this:
value="<?php _post(time); ?>"
and returns nothing. -
Treffynnon about 13 years
value="<?php echo htmlentities(_post(time)); ?>
-
Treffynnon about 13 yearsThis does not check if the variable is empty, which is fine if
$default
is always an empty string. If not then it doesn't function as asked for.