954,591 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Don't display an empty form field?

I'm using a PHP script called rwf_mail.php ( http://www.robertswebforge.com/scripts/rwf_mail.shtml) .

It creates a form that can be emailed. It consists of the php file and a plain html file to display the form.

So in the HTML page, I have code similar to:

<input size=50 name="NAME">


The PHP script pulls the $fields{"NAME"} and gets the user input from the form on the HTML page.

It then takes all of the fields defined in the PHP script and emails the field name and the form input.

What happens though is if a field is empty, it still emails the field name.

If the input from the form is blank, I don't want it to email the field name either. Basically just email me the field names with their values.

I believe this is the section of code that creates the message to email:

$message = "The following was submitted:\n\n";
 foreach($fields as $f => $v){
	$message .= sprintf("%20s:  %s\n",$v,$_REQUEST[$f]);
 }


What would I have to do to make this possible?

mindfrost82
Light Poster
38 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

So [php]$message = "The following was submitted:\n\n";
foreach($fields as $f => $v)
{
if ($v)
$message .= sprintf("%20s: %s\n",$v,$_REQUEST[$f]);
}[/php]?

Gary King
PHP/vBulletin Guru
Team Colleague
417 posts since Nov 2003
Reputation Points: 53
Solved Threads: 5
 
So [php]$message = "The following was submitted:\n\n"; foreach($fields as $f => $v) { if ($v) $message .= sprintf("%20s: %s\n",$v,$_REQUEST[$f]); }[/php]?

One problem with this solution is that if the value was 0, it would not be displayed either. So you should better use:

if ($v!=="")
php_daemon
Junior Poster
140 posts since Aug 2006
Reputation Points: 13
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You