mail() & Blank Form Fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

mail() & Blank Form Fields

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

Post by UserFriendly »

Thanks.

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

-UserFriendly
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

qads, you really should have a read of the second link in my sig.

Mac
Cruzado_Mainfrm
Forum Contributor
Posts: 346
Joined: Sun Jun 15, 2003 11:22 pm
Location: Miami, FL

Post 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" ); 

?>
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post 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:
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

Post 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
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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..
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

Post by UserFriendly »

More stuff to learn... WooHoo!

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

-UserFriendly
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

I'm not exactly sure if that's the exact coding for that.. but hope it helps
UserFriendly
Forum Newbie
Posts: 6
Joined: Thu Oct 09, 2003 8:07 am

Post 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
Post Reply