Jump to content

Make form Mail ignore empty text fields - Complete n00b


theamazingaustin

Recommended Posts

I apologize in advance, I know pretty much nothing about PHP - don't hate me, please! :P

But I'm working on a form mailer, and it functions, but what I don't like is it leaves all the text fields, blank or not, in my email, which makes it difficult for my client to read, so theres a long list of

txt field1:

another text field:

blah blah:

 

all the way down the email - and I want it gone! haha.

I've researched how to do this, but basically, I have no idea how to implement it into my code and don't have time right now to up and learn PHP.

My code is as follows:

 

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

// Subject of email sent to you.
$subject = 'PCI Tour/Excursion Request'; 

// Your email address. This is where the form information will be sent. 
$emailadd = 'MY EMAIL HERE'; 

// Where to redirect after form is processed. 
$url = 'FORWARDING URL'; 

// 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 = "Results from form:\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.'">';
?>

 

IT was a very simple copy-paste form, but I have no idea what on earth I'm doing.

If anyone has any ideas I would GREATLY appriciate the help!

Thanks!

-Austin

Link to comment
Share on other sites

I'm not sure what you're asking here.  The script is a mess.  I stripped out the complete nonsense and am left with this.  Give it a try.

 

// Subject of email sent to you.
$subject = 'PCI Tour/Excursion Request'; 

// Your email address. This is where the form information will be sent. 
$emailadd = 'MY EMAIL HERE'; 

// Where to redirect after form is processed. 
$url = 'FORWARDING URL'; 

$text = "Results from form:\n\n"; 
foreach ($_POST as $key => $value) {
  $value = trim(strip_tags($value));
  if (!empty($value)) {
     $text .= "$key: $value\n";
  }
}
mail($emailadd, $subject, $text, 'From: '.$emailadd.'');
echo '';

Link to comment
Share on other sites

I'm not sure what you're asking here.  The script is a mess.  I stripped out the complete nonsense and am left with this.  Give it a try.

 

I really appreciate that! I tried the new form but here is an example email I am getting:

 

Schedule_Jet_Ski:    on

Zipline_Date:

Zipline_Individuals:

ziplineInfo:

Fishing_Date:

Fishing_Individuals:

fishingInfo:

ATV_Date:

ATV_Individuals:

atvInfo:

Jetski_Date:          09/08/2011

Jet_Ski_Individuals:

jetskiInfo:          test

Sailing_Date:

Sailing_Individuals:

SailingInfo:

Kayak_Date:

Kayak_Individuals:

kayakingInfo:

Adventure_Date:

AdvTour_Individuals:

adventureTourInfo:

Name:

Email:

Phone:

Submit:              Submit

 

A few of these I'm making required, but the problem is all these text fields left blank show up in the email and make it such an eye sore for my client.

Is there a way to say if the field contains to data to not put it in the email? So I would end up with this instead:

 

Schedule_Jet_Ski:    on

Jetski_Date:          09/08/2011

Submit:              Submit

 

with MUCH less junk.

 

Thanks!!

-Austin

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.