How do I create a PHP order form that emails the sender what is sent?

matt2654
matt2654 used Ask the Experts™
on
I need to create an order form that the persons who order materials get a response that 1. the order was sent, and 2. a copy of the order. This is the script I have so far:


<?php
//Linecraft PHP Form mail v.1.0.0
if(isset($_POST["Submit"])){
      $mail_form="selector-order";
      $exception_fields=array("Submit");
      $to="m?????ell@f??.???";
      $from=$_POST['email'];
      $cc="";
      $bcc="";
      $subject="Book Order Form";
      $message_header="<b>Book Order Form</b><br>\r\n<hl><br>\r\n";
      $message_footer=">>>>>----END----<<<<<";
      $message_body="A Selector has sent the following order:<br><br>";
      $format_html= "\r\nContent-type: text/plain; charset=utf-8";
      $html="1";
      $skip_blank="";
      $success_page="../../about/forms/thanks.html";
      $error_page="../../about/forms/error.html";
      $o_t="";
      $c_t="";
      $br="\n";
      //If HTML
      if($html=="1"){
            $o_t="<b>";
            $c_t="</b>";
            $br="<br>";
            $format_html= "\r\nContent-type: text/html; charset=utf-8";
      }
      foreach($_POST as $fieldName => $value){
            if(!in_array($fieldName,$exception_fields) && ($value!="" || $skip_blank=="")){
                  $fieldName=str_replace("_"," ",$fieldName);
                  $message_body=$message_body.$o_t.$fieldName.$c_t.": ".$value.$br;
            }
   }
      $message=$message_header.$br.$message_body.$br.$message_footer;
      $headers="From:".$from.$format_html;
      $headers.=($cc!="")?("\r\nCc: " . $cc):"";
      $headers.=($bcc!="")?("\r\nBcc: " . $bcc):"";
      if(mail($to, stripslashes($subject), stripslashes($message),$headers)){
            if($success_page!=""){
                  header("Location:".$success_page);
            }
      }else{
            if($error_page!=""){
                  header("Location:".$error_page);
            }
      }
}
 
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<link href="/style.css" rel="stylesheet" type="text/css" />
<title>Book Selection</title>
<script type="text/javascript">
function addRowToTable()
{
  var tbl = document.getElementById('tbl1300');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  //  var iteration = lastRow + 1;
  var row = tbl.insertRow(lastRow);
  row.name="dynaRows";
  row.id = "row_"+iteration;
  //  cell 0
  var cell1 = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'title' + iteration;
  el.id = 'title' + iteration;
  el.size = 20;
  cell1.appendChild(el);

  //cell 1
  var cell1 = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'author' + iteration;
  el.id = 'author' + iteration;
  el.size = 20;
  cell1.appendChild(el);

//cell 2
  var cell2 = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'isbn' + iteration;
  el.id = 'isbn' + iteration;
  el.size = 20;
  cell2.appendChild(el);
 
   //cell 3
  var cell3 = row.insertCell(3);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'vendor' + iteration;
  el.id = 'vendor' + iteration;
  el.size = 20;
  cell3.appendChild(el);
 
  // delete button
  var cell3 = row.insertCell(4);
  var el = document.createElement('input');
  el.type = 'button';
  el.value = 'Remove';
  el.name = 'removeBtn' + iteration;
  el.id = 'removeBtn' + iteration;
  el.size = 20;
  cell3.appendChild(el);
 
  addEvents(el,"click",function(){deleteRow("row_"+iteration)},false);
}
 
// function to delete the row
function deleteRow(rowId)
{
      var rowObj = document.getElementById(rowId);
      rowObj.parentNode.removeChild(rowObj);
}
 
//function to attch event
 
function addEvents(element,type,handler,capture)
{
         if (document.addEventListener)
         {
                     element.addEventListener(type, handler, capture);
         }
         else if (document.attachEvent)
         {      
                     element.attachEvent("on" + type, handler);
         }
}
 
</script>
 
<form name="h" method="post">
<input type="hidden" name="Sort-order" value="title, author, ISBN, vendor"/>
<strong>
<label for="Order-submitted-by">Selector</label>
</strong>
<input type="text" name="Order-submitted-by" id="Order-submitted-by" />
<label for="email"><strong>Email</strong></label>
<input type="text" name="email" id="email" />
<table id="tbl1300">
  <tr>      
    <td class="txtBase"><strong>Title</strong></td>
    <td class="txtBase"><strong>Author</strong></td>
    <td class="txtBase"><strong>ISBN</strong></td>
    <td class="txtBase"><strong>Vendor</strong></td>
    <td></td>
    <tr>
      </th>
    <tr id="row_1" name="dynaRows">
        <td><input name="title" type="text" size="20" maxlength="25"></td>
        <td><input name="author" type="text" size="20" maxlength="25"></td>
        <td><input name="ISBN" type="text" size="20" maxlength="10"></td>
        <td><input name="vendor" type="text" size="20" maxlength="10"></td>
        <td></td>
    </tr>
</table>
  <input type="button" name="Add" value="Add another item row" onClick="javascript: return addRowToTable();">
  <input type="submit" name="Submit" value="Send order">
</form>
</body>
</html>
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:

  $eol="\r\n";

# To Email Address
$emailaddress="user <email@yes.com>";


# From Email Address
$fromaddress = "user <email@yes.com>";

# Message Subject
$emailsubject="Re: great news";

  $headers .= 'From: '.$fromaddress.''.$eol;
  $headers .= 'Reply-To: '.$fromaddress.''.$eol;
  $headers .= 'Return-Path: '.$fromaddress.''.$eol;    // t

$msg = "something to read";  

mail($emailaddress, $emailsubject, $msg, $headers);

Commented:
if you want to send the same thing to 2 emails then use this.

mail("email@yes.com,".$_POST['email'],$emailsubject, $msg, $headers);
Hello, I recommend you use PhpMailer for this.

http://phpmailer.worxware.com/

Author

Commented:
Hi eNarc, Thanks for posting your solution. Could you go one step more and tell me if I need to replace parts of my code with your new parts, or do I append the php code? If I append, which that is what I *think* I should do because of the *$eol="\r\n";* then wher do I do this?
Thank you for your lightning response! I really do appreciate it.
matt
Commented:
your very welcome.

the $eol="\r\n"; is needed at the end of each tag.


though from what I see in your code, you've already got the \r\n

 $headers.=($cc!="")?("\r\nCc: " . $cc):"";
      $headers.=($bcc!="")?("\r\nBcc: " . $bcc):"";

$eol="\r\n"; is only there in a string as a easier way to update if need be.

for example there at the front like this.

$format_html= "\r\nContent-type: text/html; charset=utf-8";

and the $eol just is there to make it neater. and puts it at the end like so.

$format_html= "Content-type: text/html; charset=utf-8".$eol;


from looking at your code $to="m?????ell@f??.???"; could be added like "Name <youremail@domain.com>,Name <theremail@domain.com>"; or like "Your Name <youremail@domain.com>,There Name <".$_POST['email'].">";

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial