Page 1 of 1

Want to send form via email but only inc. completed fields

Posted: Thu Aug 07, 2003 5:05 am
by lanius
Hi all,

I have a php page that has a submit form whose contents are emailed when submitted. The form is for people to submit bird records (the website is for a bird club) and I'm adapting the form to include several boxes to allow entering of more than one record, one on each row. There is a bit of Java in there too for a calendar and "required fields". At the moment it works but emails everything to specified recipients, regardless of what is entered in the boxes. The code is below and at the moment only includes two species rows for brevity.

What I want to be able to do is amend the script so that it only includes completed rows from the form. This would be decided by whether the person has entered a species name in the "rowxspecies" box. I'm a complete newbie to php so any help would be much appreciated please!

Thanks very much,
Gareth

Code: Select all

<?php

$day = date("d",time());  
$month = date("m",time());  
$year = date("Y",time());  
$grab_time=`date +%H:%M:%S`; 
$TO = "user@domain.com";
$MP = "/usr/lib/sendmail -t";

if ($formFill)&#123;
        $CHECK= strstr ($from, '@');
        $CHECK1= strstr ($CHECK, '.');
        if ( ( ereg( "(&#1111;~!#$%^&*])", $EMAIL)) || (!$CHECK) || ($CHECK == "@") ||
        (!$CHECK1) )
        &#123;
        print "Sorry $from is not a valid email address. Please click on the browser 'back' button to go back and correct it. \n Thanks!"."<br>\n";
        exit;
        &#125;
        $fd = popen($MP,"w");
        fputs($fd, "To: $TO\n");
        fputs($fd, "Cc: $CC\n");
        fputs($fd, "From: $from\n");
        fputs($fd, "Sender: $from\n");
        fputs($fd, "Subject: Website Bird Record Submission\n");
        fputs($fd, "X-Mailer: Bird Club form\n");
        fputs($fd, "Content-Type: text/html; charset=us-ascii\n");
        fputs($fd, "Content-Transfer-Encoding: 7bit\n");
        fputs($fd, "Submitted by $name at $grab_time on $day/$month/$year<br>");
        fputs($fd, "Email address - $from <br><br>"); 
        fputs($fd, "$row1species, $site, $name, $date, $row1count, $row1comment\n<br>");
        fputs($fd, "$row2species, $site, $name, $date, $row2count, $row2comment\n"); 
        pclose($fd);
        
         exit;
&#125;else&#123;
echo "<html>";
echo "<body>";
echo "

<body bgcolor=#8FBC8F LINK=#483D8B VLINK=#FFCC00>
<head>
<script language=javascript src=cal2.js>
/*
Xin's Popup calendar script- Xin Yang (http://www.yxscripts.com/)
Script featured on/available at http://www.dynamicdrive.com/
*/
</script>
<script language=javascript src=cal_conf2.js></script>

<script language="JavaScript">
<!--

/*
Required field(s) validation- By NavSurf
Visit NavSurf.com at http://navsurf.com
Visit http://www.dynamicdrive.com for this script
*/

function formCheck(formobj)&#123;
if (document.submit.name.value=='') &#123;
       alert('Please enter observer name');
       document.submit.name.focus();
       return false;
    &#125;
    if (document.submit.from.value=='') &#123;
       alert('Please enter your email address');
       document.submit.from.focus();
       return false;
    &#125; 
    if (document.submit.site.value=='') &#123;
       alert('Please select the site');
       document.submit.site.focus();
       return false;
    &#125;
    if (document.submit.date.value=='') &#123;
       alert('Please choose the date of sightings');
       document.submit.date.focus();
       return false;
    &#125;
&#125;
//-->
</script>
</head>

<p align=center><font size=+3>Submission of bird sightings</p></font>


<form name=submit method="post" action="wibble.php" onsubmit="return formCheck(this);">
<center>          <table BGCOLOR=#2F4F4F cellspacing=2 cellpadding=2 border=5>
            <tr> 
              <td align=LEFT><b><font color=white face=Arial, Helvetica, sans-serif size=2>Observer's
                name *</font></td>
              <td align=left><font face=Arial, Helvetica, sans-serif size=2>
                <input type=Text name=name size=30 maxlength=30>
                </font></td>
            </tr>
            <tr>
              <td align=LEFT><font color=white face=Arial, Helvetica, sans-serif size=2><b>Observer's
                email address *</font></b></td>
              <td align=left><font face=Arial, Helvetica, sans-serif size=2>
                <input type=Text name=from size=30 maxlength=30>
                </font></td>
            </tr>
            <tr>
              <td  align=LEFT><font color=white face=Arial, Helvetica, sans-serif size=2><b>Site name *</font></td></b>
              <TD><select name=site>
        <option></option>
        <option value=Amwell>Amwell</option>
        <option value=Startops End Reservoir>Startops End Reservoir</option></select>
        </td> 
        </tr>
        <tr>
          <td align=LEFT><font color=white face=Arial, Helvetica, sans-serif size=2><b>Date of observations *</font></td></b>
          <td><input type=text name=date size=10 onclick=javascript:showCal('Calendar1')> 
          </td></tr> 
        </table>
<center><font color=white>* denotes required field</font></center>
<br><br>
<center><table cellpadding=2 cellspacing=2 border=5>
    <tr BGCOLOR=#2F4F4F>
      <td class=normal><b><font color=#FFFFFF>Species&nbsp;</font></b></td>
      <td class=normal width=10><b><font color=#FFFFFF>Count&nbsp;</font></b></td>
      <td class=size1><b><font color=#FFFFFF>Comments&nbsp;</font></b></td>

        </TR>
        <TR>
        <TD><select name=row1species>
        <OPTION></OPTION>
        <OPTION>Auk, Little</OPTION><OPTION>Avocet</OPTION><OPTION>Bittern</OPTION><OPTION>Bluethroat</OPTION><OPTION>Duck, Mandarin</OPTION>
        </SELECT></TD>  

        <TD><input type=text name=row1count size=3 value=1></TD>

        <TD><textarea name=row1comment rows=1></textarea></td>
</TR>
        <TR>
        <TD><select name=row2species>
        <OPTION></OPTION>
        <OPTION>Auk, Little</OPTION><OPTION>Avocet</OPTION><OPTION>Bittern</OPTION><OPTION>Bluethroat</OPTION><OPTION>Duck, Mandarin</OPTION>
        </SELECT></TD>  

        <TD><input type=text name=row2count size=3 value=1></TD>

        <TD><textarea name=row2comment rows=1></textarea></td>
</TR>
        </table>
<br><br><input type=hidden name=formFill value=formmail><input type=Submit value=Submit record>
                </font></td>
              <td align=LEFT valign=TOP><font face=Arial, Helvetica, sans-serif size=2>
                <input type=reset>
        </form>

</body>
</html>";
&#125;
?>

isset and form vars

Posted: Thu Aug 07, 2003 9:37 am
by guinness
Ok, I imagine you're setting this form to post against itself. Basically, the page name in your <form> tag for action is actually the page itself. You should refer to form vars like this $_POST['variableName'] or $HTTP_POST_VARS['variableName'] depending on which version of php you're running but it sounds like you're just running on someone else's server, so I'm going to assume it's a newer version.

Ok, so you can check to see if a variable's set by using isset.

Here's how I amended your php slightly:

<?
if (isset($_POST[Submit])) //submitted?
{
$CHECK= strstr ($from, '@');
$CHECK1= strstr ($CHECK, '.');
if ((ereg( "([~!#$%^&*])", $EMAIL)) || (!$CHECK) || ($CHECK == "@") || (!$CHECK1) ) {
print "Sorry $from is not a valid email address. Please click on the browser 'back' button to go back and correct it. \n Thanks!"."<br>\n";
exit;
}
/*
not sure what you want to do if it is or isn't
just use the template below. you can do this for any variable.
I changed your variables to use $_POST below too
*/
if (isset($_POST['row1species'])) {
$row1species = $_POST['row1species'];
} else {
$row1species = "not filled in";
}
//end template
$fd = popen($MP,"w");
fputs($fd, "To: $TO\n");
fputs($fd, "Cc: $CC\n");
fputs($fd, "From: $from\n");
fputs($fd, "Sender: $from\n");
fputs($fd, "Subject: Website Bird Record Submission\n");
fputs($fd, "X-Mailer: Bird Club form\n");
fputs($fd, "Content-Type: text/html; charset=us-ascii\n");
fputs($fd, "Content-Transfer-Encoding: 7bit\n");
fputs($fd, "Submitted by $name at $grab_time on $day/$month/$year<br>");
fputs($fd, "Email address - $from <br><br>");
fputs($fd, "$row1species, $_POST['site'], $_POST['name'], $_POST['date'], $_POST['row1count'], $_POST['row1comment']\n<br>");
fputs($fd, "$_POST['row2species'], $_POST['site'], $_POST['name'], $_POST['date'], $_POST['row2count'], $_POST['row2comment']\n");
pclose($fd);
exit;
} else {
echo "<html>";
echo "<body>";
echo " ";
//yada, yada, yada, ............
?>

Remember 2 things:

1) you can access any variable after the post (form submission) by using $_POST['variableName']

2) you can test any variable after the post by using isset($_POST['variableName'])

hope this helps

Posted: Thu Aug 07, 2003 10:16 am
by m3rajk
1: look up your version of php in 4.1 superglobal started defaulting to off and the initial superglobal variables were deprecated for a short that most ppl use.

2: error checking should be done server side, or it can be circumvented. you can use client side to check, and advise, like say warn when you're nearing a text limit, but not to do error checking.

3: do you knwo the PHP_SELF variable?

4: either i mised it scrolling through skimming, or you didn't have the area yo u set up the mail call, so here's a suggestion:

Code: Select all

/*this assumes this is a form letter, there's a variable act used to see if it' submitted and a vatiable mail that's set to true if it passes error checking. and that php is 4.1.0 or higher */
if($_POST['act']){
  /* error checking */
  if($mail){
    $message='the folowing was just recieved:\n\n';
    if(isset($_POST['var1'])){ $message.="var1 :: $_POST['var1']\n"; }
    if(isset($_POST['var2'])){ $message.="var2 :: $_POST['var2']\n"; }
    if(isset($_POST['var3'])){ $message.="var3 :: $_POST['var3']\n"; }

    mail('admin@whatever.com', 'form submission', $message);
  }
}
this way you ONLY add what's filled out