用文本替换空表单字段

I have a form that users fill out, the contents of which get sent to dompdf for processing. If a user elects not to fill out a field, I want that to be reflected by "not entered" on the resulting pdf for any of the empty fields throughout the form.

I'm trying to update the fields before they're sent to dompdf for processing by doing something like this:

<?php

foreach ($_POST as $key => $value)
    {
        if(empty($value))
            {
                $_POST[$key] = "Not entered";
            }
    }
?>

But it doesn't work. It just passes on empty $value(s).

So my question is: How can I loop through the $_POST array, replacing the $value of a $key=>$value pair with, "not entered" if $value is empty?

Thank you for any help.

Isset() checks if a variable has a value including ( False , 0 , or Empty string) , But not NULL. Returns TRUE if var exists; FALSE otherwise.

On the other hand the empty() function checks if the variable has an empty value empty string , 0, NULL ,or False. Returns FALSE if var has a non-empty and non-zero value.

<?php

    foreach ($_POST as $key => $value)
        {
            if(!isset($value))
                {
                    $_POST[$key] = "Not entered";
                }
        }
    ?>

Try

<?php

foreach($_POST as &$value){
  if (empty($value)) $value = "Not entered";
}

?>

Prepending the & to $value in the foreach allows you to modify the value inline http://php.net/manual/en/control-structures.foreach.php

However, if you are using HTML forms (form tag / input elements) then the form will only submit the data for the filled in elements

You may want to look into a javascript solution or something along these lines in PHP

<?php

if (empty($_POST['name'])) $_POST['name'] = "Not entered";

?>

Note that the keys will have to be manually provided since they're not actually part of the request

If you have a large number of keys which should not be empty, then try something like

<?php

$notempty = Array('name', 'address', 'email');
foreach($notempty as $v){
  if(empty($_POST[$v])) $_POST[$v] = 'Not entered';
}

?>

This way if any of name, address, or email are empty, they will be filled in with "not entered"

Empty elements are not submitted which is why you don't see anything in your PHP code.

One alternative is to inject these values using Javascript upon form submission. You could do something like:

<form onsubmit="return yourjsfunction();" />

The JS function can gather all input elements that don't have any values entered and set their valie to "Not entered." It would then call form.submit();

Empty fields do not get submitted in the POST.

Do vardump($_POST) to see what fields get submitted. So you can test for an empty field by using isset. If the variable is not set, then you can do whatever you want with it.

if(!isset($_POST['foo'])) {
    // the field foo was empty
}

You can achieve this using php array_map function as follows,

$array = $_POST;
$array = array_map(function($val) { return isset($val) ?$val:'Not entered'; }, $array);

But doing so, will confuse the future programmers' of where it is coming from. So assign it to some variable and send it to wherever you want.

Seems to work with:

foreach ($_POST as $key => $val) {
    if (empty($val)) {
    $post -> $key = "not entered";
    }
    }

This is for an HTML form that posts to itself upon submission. (I probably should have pointed that out...).