Jump to content

Send copy, add Totals field & ignore blanks on simple php form


MacGuyUK

Recommended Posts

Hi PHPFreaks,

 

I have a very simple php script for sending an online form which is working fine, but I would like to add a few features and would be grateful if anybody could help me or at least point me in the right direction. Not even sure these features are possible at all -  I'm a total newbie!

 

I have included the php and form codes below. I want to add the following:

 

1 - Send a copy of the form to the sender [$email]

2 - Create a Totals field which will add all the numbers entered as qty1; qty2; etc

3 - Ignore (not send to recipient) all qty fields left blank (currently, these are returned as "Item1=")

4 - Have a warning dialogue window ("Are sure you want to close this window?") when user tries to close the form before submitting it.

 

Are these possible on my simple form? Any help/suggestions would be much appreciated.

 

Thank you!

 

 

PHP SCRIPT

<?php
$Company = $_REQUEST['company'];
$Ordered = $_REQUEST['ordered'];
$Email = $_REQUEST['email'];
$Date = $_REQUEST['date'];
$PO = $_REQUEST['po'];
$Message = $_REQUEST['message'];

$Item1 = $_REQUEST['qty1'];
$Item2 = $_REQUEST['qty2'];
$Item3 = $_REQUEST['qty3'];
$Item4 = $_REQUEST['qty4'];
$Item5 = $_REQUEST['qty5'];

/*Sending Email*/

$to = $_POST['to'];
$subject = "Form";
$message = "

Company = $company
Ordered = $ordered
Email = $email
Date = $date
PO = $po
Message = $message

Item1 = $qty1
Item2 = $qty2
Item3 = $qty3
Item4 = $qty4
Item5 = $qty5";
$from = "$email";

if(mail($to, $subject, $message, "From: $from"))
echo "Thank you for submitting your form";
else
echo "Mail send failure - message not sent"; 
?>

 

 

THE FORM:

 

<form action="formsend.php" method="post" enctype="multipart/form-data" name="form" class="style63" id="form">  
<table width="100%" border="0" cellspacing="5" cellpadding="0">
      
      <tr>
        <td width="4%" valign="middle" nowrap="nowrap"><div align="right">1.</div></td>
                <td width="27%" valign="middle" nowrap="nowrap">Company</td>
                <td width="52%"><input name="company" type="text" class="style63" id="company" />              </td>
              </tr>
      
      
      <tr>
        <td valign="middle"><div align="right">2.</div></td>
                <td valign="middle">Ordered by</td>
                <td><input name="ordered" type="text" class="style63" id="ordered" value="" /></td>
              </tr>
      <tr>
        <td valign="middle"><div align="right">3.</div></td>
                <td valign="middle">Email <span class="style174 style185">(*Required)</span></td>
                <td><input name="email" type="text" class="style63" id="email" value="" size="40" /></td>
              </tr>
      
      <tr>
        <td valign="middle"><div align="right">4.</div></td>
                <td valign="middle">Date</td>
                <td><input name="date" type="text" class="style63" id="date" /></td>
              </tr>
      
      <tr valign="middle">
        <td colspan="3" class="style63"><hr width="100%" size="1" noshade="noshade" /></td>
              </tr>
      
      <tr>
        <td valign="middle" class="style63"><input name="qty 1" type="text" class="style63" id="qty 1" size="3" maxlength="3" /></td>
                <td colspan="2" valign="middle">Item 1</td>
              </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty 2" type="text" class="style63" id="qty 2" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 2</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty 3" type="text" class="style63" id="qty 3" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 3</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty 4" type="text" class="style63" id="qty 4" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 4</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty 5" type="text" class="style63" id="qty 5" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 5</td>
      </tr>
      
      
      
      
      <tr>
        <td colspan="3" valign="top" class="style63"><hr width="100%" size="1" /></td>
              </tr>
      <tr>
        <td colspan="3" valign="top" class="style63"><div align="center" class="bodytext_lg"><strong>Please use the drop-down menu below to select your Recipient then click the Submit button</strong></div></td>
              </tr>
    </table>
    <label></label>
            <div align="center">              </div>
    <label class="style63"></label>
    <label>
    <div align="center">
      <select name="to">
        <option value="[email protected]">Recipient1</option>
        <option value="[email protected]">Recipient2</option>
        <option value="[email protected]">Recipient3</option>
      </select>
      <input name="Submit" type="submit" class="style63" id="Submit" value="Submit" />
      <br />
      <br />
  </div>    </label>
</form>

Hi MacGuyUK,

 

These are indeed possible to do.  I hope I can help a little here.

 

1 - It looks like you already have an example of mail() running, but of course check out mail() in the PHP manual for further examples of how to use this.  Especially if you want to send as HTML emails, etc. for formatting.

 

2 - The simplest way to create a totals field would to be simply to add them together, e.g.:

 

$total_qty = $Item1 + $Item2 + $Item3 + $Item4 + $Item5;

 

Assuming that these fields all contain numbers, this should give a total of all fields.

 

3 - Ignoring the empty variables I would do something like this:

$message = "
Company = $company
Ordered = $ordered
Email = $email
Date = $date
PO = $po
Message = $message ";

$qtyArray = array($qty1, $qty2, $qty3, $qty4, $qty5);
$cnt = 0;

while ($cnt <= count($qtyArray))
{
    if ($qtyArray[$cnt] > 0)
        $message .= "Item" . $cnt . " = " . $qtyArray[$cnt] ;

   $cnt++;
}

 

This will create your first part of the $message then it creates an array storing all of the quantities so they are available as:

  $qtyArray[0]

  $qtyArray[1]

  $qtyArray[2]

  $qtyArray[3]

  $qtyArray[4]

 

The counter ($cnt) will look at the first value in the array, ensure it doesn't equal 0 and if it doesn't then it appends it to $message.  This loops through all of the array items to do the same thing, ignoring all that equal 0.  Of course you may need to do something as the $qty may equal " " so you would need to adjust this accordingly.  But this should help.. or be a starter.

 

4 - The dialogue box would most likely need to be done in JavaScript.  You should be able to find lots of info on Google about this.

 

I hope this helps.

 

Good luck

Hi jd307,

 

I really appreciate your suggestions.

 

I wanted to add the fields on the actual form as the user enters the qty and your suggestion only added them on the returned message, but that really helped me and I think I have now solved the totals field on the form with a JavaScript.

 

As for the empty variables, that doesn't seem to be working. Also still struggling with the mail() function to include a cc...

 

I think you're right about the dialogue box, so I'm gonna look into that next.

 

Thank you very much for your help, but if you or anybody else has any more ideas, I'd be very grateful.

I know what it's like looking for help. I've just started out with PHP and every question that I have answered turns into 5 more unawared questions. Nevertheless, it's become an obsession. I have to refrain from posting multiple questions here as I don't want to piss anyone off. I didn't even work today. I spent most of it reading about arrays and strings.

 

In any event, I don't know much about PHP but I do know how to process forms effectively. So I'd like to help someone here for once.

 

Try this:

<form action="formsend.php" method="post" name="form" class="style63" id="form"> 
<table width="100%" border="0" cellspacing="5" cellpadding="0">
     
      <tr>
        <td width="4%" valign="middle" nowrap="nowrap"><div align="right">1.</div></td>
                <td width="27%" valign="middle" nowrap="nowrap">Company</td>
                <td width="52%"><input name="company" type="text" class="style63" id="company" />              </td>
              </tr>
     
     
      <tr>
        <td valign="middle"><div align="right">2.</div></td>
                <td valign="middle">Ordered by</td>
                <td><input name="ordered" type="text" class="style63" id="ordered" value="" /></td>
              </tr>
      <tr>
        <td valign="middle"><div align="right">3.</div></td>
                <td valign="middle">Email <span class="style174 style185">(*Required)</span></td>
                <td><input name="email" type="text" class="style63" id="email" value="" size="40" /></td>
              </tr>
     
      <tr>
        <td valign="middle"><div align="right">4.</div></td>
                <td valign="middle">Date</td>
                <td><input name="date" type="text" class="style63" id="date" /></td>
              </tr>
     
      <tr valign="middle">
        <td colspan="3" class="style63"><hr width="100%" size="1" noshade="noshade" /></td>
              </tr>
     
      <tr>
        <td valign="middle" class="style63"><input name="qty1" type="text" class="style63" id="qty1" size="3" maxlength="3" /></td>
                <td colspan="2" valign="middle">Item 1</td>
              </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty2" type="text" class="style63" id="qty2" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 2</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty3" type="text" class="style63" id="qty3" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 3</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty4" type="text" class="style63" id="qty4" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 4</td>
      </tr>
      <tr>
        <td valign="middle" class="style63"><input name="qty5" type="text" class="style63" id="qty5" size="3" maxlength="3" /></td>
        <td colspan="2" valign="middle">Item 5</td>
      </tr>
     
     
     
     
      <tr>
        <td colspan="3" valign="top" class="style63"><hr width="100%" size="1" /></td>
              </tr>
      <tr>
        <td colspan="3" valign="top" class="style63"><div align="center" class="bodytext_lg"><strong>Please use the drop-down menu below to select your Recipient then click the Submit button</strong></div></td>
              </tr>
    </table>
    <label></label>
            <div align="center">              </div>
    <label class="style63"></label>
    <label>
    <div align="center">
      <select name="to">
        <option value="[email protected]">Recipient1</option>
        <option value="[email protected]">Recipient2</option>
        <option value="[email protected]">Recipient3</option>
      </select>
      <input name="Submit" type="submit" class="style63" id="Submit" value="Submit" />
      <br />
      <br />
  </div>    </label>
</form>

 

I had to revise the code here some what. I've had issues pulling in data with spaces in it - I don't know why...

 

<?php

//Get Data From Form
$Company = $_REQUEST['company'];
$Ordered = $_REQUEST['ordered'];
$email = $_REQUEST['email'];
$Date = $_REQUEST['date'];
$TO = $_REQUEST['to'];
$Message = $_REQUEST['message'];
$qty1 = $_REQUEST['qty1'];
$qty2 = $_REQUEST['qty2'];
$qty3 = $_REQUEST['qty3'];
$qty4 = $_REQUEST['qty4'];
$qty5 = $_REQUEST['qty5'];

//Make Sure They Gave you a valid email - This will check to see if the domain is valid
function validate_email($email)
{

   // Create the syntactical validation regular expression
   $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";

   // Assume that the email is invalid
   $valid = 0;

   // Validate the Syntax
   if (eregi($regexp, $email))
   {
      list($username,$domaintld) = split("@",$email);
      // Validate the Domain
      if (getmxrr($domaintld,$mxrecords))
         $valid = 1;
   } else {
      $valid = 0;
   }

   return $valid;

}

if (validate_email($email)) {
   // email is valid
$email = $email;
} else if (empty($email)){
   // email field is empty
   print "Your email address came back empty, please click the back button and try again";
} else {
   // email is invalid
   print "Your email address came back as invalid, please click the back button and try again";
   exit;
}

//Add totals to Items
$GrandTotal = $qty1 + $qty2 + $qty3 + $qty4 + $qty5;

//Prepare the email to send

$mailTo = "[email protected], [email protected], [email protected]";
$msgSubject = "Yo, We Got An Order!! \n"; 
$msgBody = "The Following User Wrote\n

Company: $Company\n
Ordered: $Ordered\n
Email: $email\n
Date: $Date\n
To: $TO\n
Message: $Message\n
Item1: $qty1\n
Item2: $qty2\n
Item3: $qty3\n
Item4: $qty4\n
Item5: $qty5\n
Big Total: $GrandTotal\n";

//Send the email
$xHeaders = "From:$email\nX-Mailer: PHP/" . phpversion();

mail ($mailTo, $msgSubject, $msgBody, $xHeaders); 

//redirect user to a happy happy page saying that the form processed
header ("Location: contact-success.php");

?>

 

This grabs all the data, validates the email (by domain) and redirects the user to a "contact-success" page. I also threw in the grand total at the bottom within the email message. If you don't want to create a "contact-success" page then just redirect them to the main URL.

 

Cheers.  :D

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.