Page 1 of 1

Eliminating "empty" fields from form email

Posted: Sun Apr 13, 2008 10:42 pm
by awa
Do you have any ideas how I can make sure that any fields without an entry are prevented from being added to this form email?

The only way I know of doing it is

Code: Select all

   if(strlen($value[$z])>0)
     $MsgBody .= "$Field: $value\n\n";
But in this form, items are grouped into pairs, or sometimes threes, (and there are loads of them) so this doesn't work. I can't get my head around what I may need to do - can any of you kind souls help?

Code: Select all

 
<?php
 
 foreach($_POST as $key => $value){
        if (is_array($value)) {
            $_values[$key] = join("%,% ",$value);
        }else $_values[$key] = $value;
      $_values[$key]=stripslashes($_values[$key]);
      }
 
 if (!isset($_POST["_referer"])) {
    $_referer = $HTTP_SERVER_VARS["HTTP_REFERER"];
 }else $_referer = $_POST["_referer"];
 
 function Display_Selected($_field, $_value){
     $array = split("%,% ",$_field);
     foreach($array as $key){
        if ($key == $_value) {
            echo "selected";
            break;}}}
 
 
 function Display_Radio($_field, $_value){
     $st =  "<input type=\"radio\"";
     if (isset($_field)){if ($_field == $_value) {
        $st .=  " checked";}}
     $st .=  " disabled>";
     return $st;}
 
 
 function IsThereErrors($form) {
  global $_POST, $_FILES, $_values;
  $req[0][] = "Name";
  $req[0][] = "Business_name";
  $req[0][] = "Address";
  $req[0][] = "Email";
  $email[0][] = "Email";
  $req[0][] = "Telephone";
  $req[0][] = "FAO";
  $flag = false;
 
  for($i=0;$i<Count($req[$form]);$i++){
     if ($_POST[$req[$form][$i]]=="") {
       $flag = true;}}
 
  for($i=0;$i<Count($email[$form]);$i++){
     if (!preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/',$_POST[$email[$form][$i]]) && $_POST[$email[$form][$i]]!=""){
       $flag = true;}}
 
 return $flag;}
 
 function display_errors($form, $_isdisplay) {
  global $_POST, $_FILES;
  $req[0][] = "Name";
  $req[0][] = "Business_name";
  $req[0][] = "Address";
  $req[0][] = "Email";
  $email[0][] = "Email";
  $req[0][] = "Telephone";
  $req[0][] = "FAO";
  if ($_isdisplay) {
     echo "<p align=\"center\"><font color=\"red\" face=\"Verdana, Arial, Helvetica, sans-serif\" Size=\"2\">";
 
     for($i=0;$i<Count($req[$form]);$i++){
          if ($_POST[$req[$form][$i]]=="") {
               echo "<br> ERROR: Field <b>".$req[$form][$i]."</b> is required!";}}
 
       for($i=0;$i<Count($email[$form]);$i++){
          if (!preg_match('/^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,6}|[0-9]{1,3})(\]?)$/',$_POST[$email[$form][$i]]) && $_POST[$email[$form][$i]]!=""){
            echo "<br> ERROR: <b>'".htmlspecialchars($_POST[$email[$form][$i]])."'</b> is not valid email address!";}}
 
        echo "</font></p>";}}
 
 
// Snip all the required fields, HTML, etc...
 
// Return at beginning of notifier email
 
 function SendEmails(){
    global $_values, $zag, $un;
    $un        = strtoupper(uniqid(time()));
    $to[0]      .= "nnnnn@nnnnnnn.co.uk";
    $subject[0]      .= "Quotation";
    $head[0]      = "From: ".str_replace("%,%", ",", $_values['Email'])."\n";
    $head[0]      .= "Subject: Quotation\n";
    $head[0]      .= "Reply-To: ".str_replace("%,%", ",", $_values['Email'])."\n";
    $head[0]     .= "Content-Type:multipart/mixed;";
    $head[0]     .= "boundary=\"".$un."\"\n\n";
    $EmailBody = "A quotation has been requested for the following: 
    
 
// And this is an example of one of the items put into the email body
 
Galvanised Sheets: 
".str_replace("%,%", ",", $_values['1_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['1_Galvanised_Sheets_Size'])."
".str_replace("%,%", ",", $_values['2_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['2_Galvanised_Sheets_Size'])."
".str_replace("%,%", ",", $_values['3_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['3_Galvanised_Sheets_Size'])."
 
// followed by
 
BuildBody($EmailBody, False, 1);
     for ($i=0;$i<=1;$i++){
      mail($to[$i], $subject[$i], $zag[$i], $head[$i]);
    }
  }
  
  
 $actions = array ("display_quote","display_preview","redirect_to");
 
 
  if (isset($_POST["_next_page"])) {
    $_next_page = $_POST["_next_page"];
 }else $_next_page = 0;
 
 if ($_POST["submit"]=="back") {
    call_user_func($actions[$_next_page-2],false);
 }else if (IsThereErrors($_next_page-1)){
             call_user_func($actions[$_next_page-1],true);
          }else {
              call_user_func($actions[$_next_page+0],false);
              if ($_next_page == count($actions)-1) {
                SendEmails();
              }
           }
?>

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 3:56 am
by awa
I am also being asked for further amends...

Code: Select all

 
Galvanised Sheets:
".str_replace("%,%", ",", $_values['1_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['1_Galvanised_Sheets_Size'])."
".str_replace("%,%", ",", $_values['2_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['2_Galvanised_Sheets_Size'])."
".str_replace("%,%", ",", $_values['3_Galvanised_Sheets_Quantity'])     ."".str_replace("%,%", ",", $_values['3_Galvanised_Sheets_Size'])."
 
Each item is configured as above - using this example, Galvanised Sheets is a heading where all the items chosen list below in the generated emails. All of the different headings appear regardless of any input in that section.

In the first post, where I was trying to not have the blank lines writing under each header if those options were not selected, I am now being asked for the header to not appear if none of the options below it are chosen.

Is this possible?

Thanks

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 5:10 am
by onion2k
Of course it's possible. You just have to check the incoming variables and if any of them are set display that section. Use an if().

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 5:14 am
by awa
If only it were that straightforward ;) I'm afraid it isn't my script and I am still fairly new to PHP... (sorry, another one of them).

Is it possible you would be able to give any more clues?

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 5:20 am
by onion2k
I think you'd be best off working through a few PHP tutorials before trying to modify some production code without even understanding how to use an if(). There's a good chance you'd break the website and not know how to fix it again.

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 5:30 am
by awa
Well, I'm not daft enough not to keep copies of the files so I don't break it!

But, you're right - I understand the basics of what an if() is and I have already made a substantial amount of changes to the script as it stands, but I will be the first to admit I do need to do a hell of a lot more learning. Unfortunately this all needs doing yesterday and I've been dropped in as doing a so called 'favour' to one of our clients to keep theirs happy, hence was just looking for a bit of advice to help me along the way.

Re: Eliminating "empty" fields from form email

Posted: Mon Apr 14, 2008 5:48 pm
by Chris Corbyn

Code: Select all

if (!empty($variableName)) {
  //display it
}

Re: Eliminating "empty" fields from form email

Posted: Fri Apr 18, 2008 6:37 am
by awa
Thanks for that, Chris. I'm still struggling, though. Is it because I am trying to get this into the email function?

I have tried several variations on

Code: Select all

 
  function SendEmails(){
    global $_values, $zag, $un;
    $un        = strtoupper(uniqid(time()));
    $to[0]      .= "...@......co.uk";
    $subject[0]      .= "Quotation";
    $head[0]      = "From: ".str_replace("%,%", ",", $_values['Email'])."\n";
    $head[0]      .= "Subject: Quotation\n";
    $head[0]      .= "Reply-To: ".str_replace("%,%", ",", $_values['Email'])."\n";
    $head[0]     .= "Content-Type:multipart/mixed;";
    $head[0]     .= "boundary=\"".$un."\"\n\n";
    $EmailBody = "A quotation has been requested for the following: 
    
Name: ".str_replace("%,%", ",", $_values['Name'])."
Business name: ".str_replace("%,%", ",", $_values['Business_name'])."
Address: ".str_replace("%,%", ",", $_values['Address'])."
Email: ".str_replace("%,%", ",", $_values['Email'])."
Telephone: ".str_replace("%,%", ",", $_values['Telephone'])."
FAO: ".str_replace("%,%", ",", $_values['FAO'])."
 
Non-standard items: ".str_replace("%,%", ",", $_values['Non-standard_items'])."
 
     // here is the line with the problem
". if (!empty ($_values['1_Angle_Equal_Quantity'])){echo 'Angle Equal: '}."
    // 
 
".str_replace("%,%", ",", $_values['1_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['1_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['2_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['2_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['3_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['3_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['4_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['4_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['5_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['5_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['6_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['6_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['7_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['7_Angle_Equal_Size'])."
".str_replace("%,%", ",", $_values['8_Angle_Equal_Quantity'])."      ".str_replace("%,%", ",", $_values['8_Angle_Equal_Size'])."
}
All I get is a 'Parse error'