PDA

View Full Version : Form results, if empty should not send


Finland Blue
09-08-2011, 04:38 PM
I have a Form, which uses PHP, and it works fine. I get the results I want to my email

My problem is that in the form the are many optional text fields which can be left blank. If they are left blank I don't want any reference to them in the email I get. now, I get the field textarea name, followed by a ":", then nothing next to it. Its a fairly long list of empty fields, so I would like it that they just don't show at all in the email if they are not filled in on the form.

Is it possible?

Ricky55
09-08-2011, 08:55 PM
Yes I would have thought so. Most things are possible with PHP.

You would need to check if the value is set only if this is true display it in the form.

Its beyond my level of knowledge of PHP though, all my PHP scripts just leave the field blank in the email.

You may be better asking on a dedicated PHP forum such PHP Freaks if you want to get a quick answer.

http://www.phpfreaks.com/

Some of the guys on there really know their PHP.

HTH

Ps
Not too sure whats wrong with leaving it blank though personally.

edbr
09-09-2011, 01:55 AM
where the message is constructed use if conditions

for example if(!empty($var)){........}

Finland Blue
09-09-2011, 07:25 AM
Not too sure whats wrong with leaving it blank though personally.

Its because the email I get back prints out to a 2nd page, because of the empty fields, and its much more clear to get just the information I need, rather than blank fields on each side ...

Finland Blue
09-09-2011, 07:31 AM
where the message is constructed use if conditions

for example if(!empty($var)){........}

Thanks for that :

- do I paste this "as it is" or I have to out something in where you have ........ ?
- and where do I paste it?

edbr
09-09-2011, 08:02 AM
no it depends on your processing script and how the message is formed $var was just a general 'description.

Finland Blue
09-09-2011, 09:14 AM
no it depends on your processing script and how the message is formed $var was just a general 'description.

Here is the code of the .php page

<?php
//--------------------------Set these paramaters--------------------------

// Subject of email sent to you.
$subject = 'My subject';

// Your email address. This is where the form information will be sent.
$emailadd = 'me@me.com';

// Where to redirect after form is processed.
$url = 'http://www.redirectedpage.htm';

// Makes all fields required. If set to '1' no field can not be empty. If set to '0' any or all fields can be empty.
$req = '0';

// --------------------------Do not edit below this line--------------------------
$text = "The following information has been submitted:\n\n";
$space = ' ';
$line = '
';
foreach ($_POST as $key => $value)
{
if ($req == '1')
{
if ($value == '')
{echo "$key is empty";die;}
}
$j = strlen($key);

if ($j >= 20)
{echo "Name of form element $key cannot be longer than 20 characters";die;}
$j = 20 - $j;
for ($i = 1; $i <= $j; $i++)
{$space .= ' ';}
$value = str_replace('\n', "$line", $value);
$conc = "{$key}:$space{$value}$line";
$text .= $conc;
$space = ' ';
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>