Suppressing Blank Fields with Novice Form (nvform.php)

I’ve used Seth Knorr’s Novice Form PHP script (nvform.php) on a few web sites to do the HTML form to email processing because it is an easy way to implement that common task. I’ve made a few changes to the script for my own use, such as adding a section to combat form spam. Recently a client asked me if I could have the email only list the fields that were filled in. On his web site we are using the script to process a form with a lot of fields, and the email ended up being 4 pages long when printed.

I figured this would be an easy change but when I looked at the code, it wasn’t obvious to me where to make the change. So I went to Google for an answer, and while I found others asking the same question in various forums, no one had answered the question.

So back to the code I went, studying it for where it was writing the email text. One thing about PHP is that you aren’t forced to write the code following any rules for line breaks, indents and such, so following someone else’s writing style can sometimes throw you. In this case I found an ELSE clause at the end of a line that I missed the first few times while reading the code.

Once I noticed that, the change I needed to make was very simple, and here is the answer. Find this line of code:

if ( $NVPOST_name == "subject" || $NVPOST_name == "sort" || $NVPOST_name == "required" || $NVPOST_name == "success_page"){}else{

and change it to this:

if ($NVPOST_value == "" || $NVPOST_name == "subject" || $NVPOST_name == "sort" || $NVPOST_name == "required" || $NVPOST_name == "success_page"){}else{

That’s all there is to it!

Personally I would have written the statement like this, which is easier for me to see the logic.

if ($NVPOST_value == "" || $NVPOST_name == "subject" || $NVPOST_name == "sort" || $NVPOST_name == "required" || $NVPOST_name == "success_page"){     /* don't print anything */ } else {     $SEND_prnt .= "$NVPOST_name; $NVPOST_value n"; }