Jump to content

Eliminating "empty" fields from form email


AW811

Recommended Posts

Hi - I have posted this in another forum so please excuse if you've seen it before - I just need to try and get this sorted!

 

 

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

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?

 

<?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]      .= "[email protected]";
    $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:
   

// Snip lots and lots of entries.... 
// 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();
              }
           }
?>

Hi Ginger

 

OK, that means that the fields are in groups of, e.g. Quantity and Size. Quantity is a text box and Size a dropdown selector with a blank first option, so it should remain blank if not selected.

 

THe problem I have with using a simple 'if field is not filled in, don't show it on the email' is that with these groups there are a couple of spaces to separate the entries (the client actually wants an 'x' putting in there), so no line is actually 'empty' as such.

 

Does that make more sense?

 

 

I am going to hazard the guess then that you did not write this script, and have no idea about PHP?

 

I cannot tell you where this goes exactly in your code because the variables $msgbody, $value and $field do not exist within it, therefore where did your original expression of,

 

if(strlen($value[$z])>0)
$MsgBody .= "$Field: $value\n\n";

 

Come from?

 

 

But basically you would evaluate the POST variables from your form before even reaching the message body of your email to prevent blanks.

 

if ($_POST['some_variable']!=="") {
//add this to my message variable
$message .= htmlentities($_POST['some_variable'], ENT_QUOTES);
}

 

Or something similar.

No, I didn't write it and my knowledge is very basic. We (as in the chaps I work with) have a couple of scripts that does what they do and we can meddle around with to an extent, but we now have a client who wants all manner of things coming from out of it.

 

THe expression I wrote out before was one used in something else as a similar idea of what I thought I meant.

 

 

 

THe problem I have with using a simple 'if field is not filled in, don't show it on the email' is that with these groups there are a couple of spaces to separate the entries (the client actually wants an 'x' putting in there), so no line is actually 'empty' as such.

 

Then you could do something like this,

 

if ($_POST['some_variable']!=="") {
//add this to my message variable
$message .= htmlentities($_POST['some_variable'], ENT_QUOTES);
} else {
$message .= "x";
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.