Page 1 of 1

mail() & Blank Form Fields

Posted: Thu Oct 09, 2003 8:07 am
by UserFriendly
Hi.

This form:
http://cgi.southeringtons.plus.com/php/form.htm

Submits to this script:
http://cgi.southeringtons.plus.com/php/sendmail.txt

Please take a look at the code, particulary 'sendmail.txt'. A glance at the form web page will be useful aswell.

It's the mail() part of the script that I need help with. Here's a snippet of my code:

Code: Select all

mail("richard@southeringtons.co.uk",
       "Southeringtons Web Enquiry",
       "
        Name: $Title $FirstName $LastName

        E-Mail Address: $email

        Address1: $Address1
        Address2: $Address2
        Address3: $Address3
        Address4: $Address4
        Address5: $Address5
        Address6: $Address6
        Address7: $Address7

     ...snipped lots more code (see url above for the rest)...

        OA_Office: $OA_Office
        OA_Loft: $OA_Loft
        OA_Garage: $OA_Garage
        OA_Garden: $OA_Garden
        OA_AnywhereElse: $OA_AnywhereElse",
       "From: $FirstName $LastName <$email>" );

  header( "Location: http://cgi.southeringtons.plus.com/php/form.htm" );
?>
I want the text such as "Address1:", "Address2:" etc... to display only if there is actually something in the variables "$Address1", "$Address2" etc...

I know I could omit the "Address1:" text and just have the contents of the variables displayed, but I'll still end up with a blank line there if the textbox wasn't filled in. The idea behind this is to reduce the number of pages the email prints out on as it is unlikly that anyone will ever fill in all the form fields.

I hope I've explained this well enough. Let me know if you need more info.

-UserFriendly

Posted: Thu Oct 09, 2003 8:39 am
by JayBird
how about this.

Might be a shorter way of doing it, but gives you an idea i hope

Code: Select all

&lt;?

if (!empty($address1)) {
	$email_body .= "Address1: $address1\n";
}
if (!empty($address2)) {
	$email_body .= "Address2: $address2\n";
}
if (!empty($address3)) {
	$email_body .= "Address3: $address3\n";
}
if (!empty($address4)) {
	$email_body .= "Address4: $address4\n";
}
if (!empty($address5)) {
	$email_body .= "Address5: $address5\n";
}
if (!empty($address6)) {
	$email_body .= "Address6: $address6\n";
}
if (!empty($address7)) {
	$email_body .= "Address7: $address7\n";
}

mail("richard@southeringtons.co.uk", 
       "Southeringtons Web Enquiry", 
       " 
        Name: $Title $FirstName $LastName 

        E-Mail Address: $email 

        $email_body

     ...snipped lots more code (see url above for the rest)... 

        OA_Office: $OA_Office 
        OA_Loft: $OA_Loft 
        OA_Garage: $OA_Garage 
        OA_Garden: $OA_Garden 
        OA_AnywhereElse: $OA_AnywhereElse", 
       "From: $FirstName $LastName &lt;$email&gt;" ); 

  header( "Location: http://cgi.southeringtons.plus.com/php/form.htm" ); 
  
  ?&gt;
Mark

Posted: Thu Oct 09, 2003 10:25 am
by UserFriendly
Thankyou, that's a great help.

This is my first go at PHP... I knew I had to do something along those lines, but wasn't aware of the fact that you could add to variables as you went along (is that what the .= does?).

The above code will certainly mean that I will be able to use the script to acheive what I wanted.

What I'm hoping for eventually is to be able to format the email with html which will either solve my problem or make it worse. I don't think the above trick will allow me to do any formatting such as putting the information in tables etc while still leaving out the blank fields.

If anyone has any more ideas they'd be appreciated.

-UserFriendly

Posted: Thu Oct 09, 2003 10:40 am
by JayBird
yes, that is what the .= does.

You could easily modify the above to include formatting by putting the addresses in table rows. If there is no address don't add a row, if there is, do add a row.

Easy

Mark

Posted: Thu Oct 09, 2003 10:44 am
by UserFriendly
Thanks.

I'll have a play about and see what happens.

-UserFriendly

Posted: Thu Oct 09, 2003 11:07 am
by qads

Code: Select all

<?php
foreach($_POST as $key => $value)
{
if(!empty($value))
{
$message.= "$key: $value\n";
}
}
$email = $_POST[email]
$FirstName = $_POST[FirstName];
$LastName = $_POST[LastName];

mail("richard@southeringtons.co.uk","Southeringtons Web Enquiry",$message,"From: $FirstName $LastName <$email>"); 
header( "Location: http://cgi.southeringtons.plus.com/php/form.htm" );

?>
with this example, you can add more fields to the form without having to edit the php code :D.

Posted: Fri Oct 10, 2003 5:38 am
by twigletmac
qads, you really should have a read of the second link in my sig.

Mac

Posted: Fri Oct 10, 2003 3:09 pm
by Cruzado_Mainfrm
i have a better version for qads code, that should work the same way, just less coding

Code: Select all

<?php 
foreach($_POST as $key => $value) 
{ 
if(!empty($value)) 
{ 
$message.= "$key: $value\n"; 
} 
} 
extract($_POST);

mail("richard@southeringtons.co.uk","Southeringtons Web Enquiry",$message,"From: $FirstName $LastName <$email>"); 
header( "Location: http://cgi.southeringtons.plus.com/php/form.htm" ); 

?>

Posted: Fri Oct 10, 2003 6:59 pm
by qads
i know twig...this is just a habbit...freaking hard to break, have to force myself to use $array['bla']....will take a while to get use too :?

Cruzado_Mainfrm, u took out 2 lines and call it less coding? :P :lol:

Posted: Tue Oct 28, 2003 9:35 am
by UserFriendly
Here's what I've gone with:

http://cgi.southeringtons.plus.com/sendmail.txt

Long & complicated, but gives me a lot more control over how the email looks. Plus I learnt a lot more while doing it :D

Thanks for all your help.

-UserFrienldy

Posted: Tue Oct 28, 2003 2:14 pm
by d3ad1ysp0rk
Wouldn't it be easier to loop it?

Code: Select all

int i=1;
while ($address[i]!=NULL)
{
  $body .="<tr><td colspan=2>$Address[i]</td></tr>";
  i++;
}
that kind of thing could be done for all the repetitive things..

Posted: Tue Oct 28, 2003 2:45 pm
by UserFriendly
More stuff to learn... WooHoo!

That looks pretty useful. I'll give it a go.

-UserFriendly

Posted: Tue Oct 28, 2003 3:22 pm
by d3ad1ysp0rk
I'm not exactly sure if that's the exact coding for that.. but hope it helps

Posted: Wed Oct 29, 2003 9:48 am
by UserFriendly
I've done a lot of trial & error and found that this works as a loop for doing the addresses:

Code: Select all

for ($n = 1; $n < 8;  $n++){
  eval("\$CAddress = \$Address$n;");
  if($CAddress){
    $body .= "<tr><td colspan=2>$CAddress</td></tr>";
  }
}
See the rest here: http://cgi.southeringtons.plus.com/sendmail.txt

-UserFriendly