PHP提交表单数据和电子邮件

时间:2017-09-27 16:38:03

标签: php html forms

我有这个网站需要输入表单数据,最初是要导出然后让用户发送电子邮件,但我总是过火,现在这就是我所拥有的,我希望这个表单数据提交文件并通过电子邮件提交文件我得到一个文件丢失错误,我使用Mamp进行服务器测试。感谢任何帮助,多年来一直在阅读这个网站,但最终开始使用它。 我有一些整洁,但不确定你需要什么。单击提交按钮后,将打开一个页面,其中未找到"文件"错误。 这是网站数据:

<!doctype html>
<html>

<meta charset="utf-8">
<title>MID Inspection Form</title>

   <link href="css/style.css" rel="stylesheet" type="text/css">
   <style>
    body {background-image: url(images/bg3.png);
        background-repeat: no-repeat;
        background-color: #cddbe7;




       }
    </style>


    <script>
function myFunction() {
    var x = document.getElementById("form1");
    x.disabled = true;
}
</script>



    <script src="js/gen_validatorv31.js" type="text/javascript"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        <script type="text/javascript">
  $(function() {
    $('#datetimepicker2').datetimepicker({
      language: 'en',
      pick12HourFormat: true
    });
  });
</script>


<form method="post" name="`enter code here`Inspection Form" action="php-form-action.php" enctype="multipart/form-data">
<head>    
</head>

<body>
<?php
if(!empty($errors))
{
    echo nl2br($errors);
}
?>


<h3>
            <p>&nbsp;</p>
<p>&nbsp;</p>

<p>
    <input type="reset" name="reset" id="reset" value="Reset Form">
    Attach Inspector's Report!
    <input type="file" id="form1" multiple placeholder="Attach Report">
    <input type="submit" Submit>
    <!--<input type="button" onclick="tableToExcel('midTable', 'MID inspection table')" value="Export to Excel">-->
    <!-- export to excel option to be added -->
</p>
    </h3>
<div class="date col-md-6 col-xs-6 col-lg-4">  
    <p>
    <label for="date" class="col-lg-1 control-label">Inspection Date:  </label>
    <label><input type="date" name="date" required title="(required)"></label>


    Location 
    <select class="form-control styled-select" name="location" id="location" required title="(required)">
      <option value="">--select--</option>
      <option value="SG 440">Abbeville, SC</option>
      <option value="SVC851">Agrock, FL</option>
      <option value="BG 130">Akron, OH</option>
      <option value="QCP  7">Albany Port, NY</option>
      <option value="PLE 19">Aliquippa, PA</option>
      <option value="S    5">Ampthill, VA</option>
      <option value="QC 178">Amsterdam, NY</option>
      <option value="SH 365">Andrews, SC</option>
      <option value="BAA 10">Annapolis Jct, MD</option>
      <option value="QRX  9">Apiny, NJ </option>
      <option value="0BA279">Ardmore, AL </option>
      <option value="BED 13">Armco Junction, OH</option>
      <option value="CA 520">Ashland, KY</option>
      <option value="QD 127">Ashtabula, OH</option>
      <option value="000294">Athens, AL </option>
      <option value="SG 506">Athens, GA</option>
      <option value="00H277">Atkinson, KY</option>
      <option value="YYG171">Atlanta Hulsey, GA </option>
      <option value="ANB865">Atlanta, GA</option>
      <option value="QN   0">Attleboro, MA</option>
      <option value="SX 820">Auburndale, FL</option>
      <option value="AK 459">Augusta, GA</option>
      <option value="00F375">Avalon St, TN</option>
      <option value="QS   9">Avon, IN</option>
      <option value="AN 588">BRUNSWICK JCT CP GA</option>
      <option value="S  653">Baldwin, FL</option>
      <option value="BAK 88">Balt Bay View, MD</option>
      <option value="BAO  8">Balt Curtis Bay, MD</option>
      <option value="BAM  3">Balt Locust Point, MD</option>
      <option value="BAL  5">Balt Penn Mary Exim, MD</option>
      <option value="BAL  3">Balt Sparrows Point, MD</option>
      <option value="BAA  3">Balt West Baltimore, MD </option>
      <option value="BAA  1">Baltimore, MD</option>
      <option value="A  111">BattleBoro, NC</option>
      <option value="QRR  6">Bayonne, NJ</option>
      <option value="QRC 11">Bayway, NJ</option>
      <option value="QM 226">Beauharnois, PQ</option>
      <option value="000ZA3">Bedford Park, IL</option>
      <option value="QC 296">Belle Isle, NY</option>
      <option value="S    9">Bellwood, VA</option>
      <option value="BAK 51">Belvedere, MD</option>
      <option value="QL 134">Benning, DC </option>
      <option value="CG  86">Benton Harbor, MI</option>
      <option value="BN   5">Benwood, WV</option>
      <option value="0LB405">Bessemer, AL</option>
      <option value="AZA890">Big Bend, FL</option>
      <option value="000007">Big Ditch CP, KY</option>
      <option value="CA 514">Big Sandy JCT, KY</option>
      <option value="000389">Birmingham, AL</option>
      <option value="SO 631">Blount Island, FL</option>
      <option value="Z  245">Bostic Yard, NC </option>
      <option value="SF 404">Bostic, NC</option>
      <option value="QB   5">Boston Beacon Park, MA</option>
      <option value="000388">Boyles, AL</option>
      <option value="AZA916">Bradenton, FL</option>
      <option value="QNB  0">Braintree, MA</option>
      <option value="Z  259">Brice, NC</option>
      <option value="00J123">Bridgeport, AL </option>
      <option value="QRZ  1">Brills, NJ</option>
      <option value="BN  38">Brooklyn Jct, WV</option>
      <option value="00F319">Brownsville, TN </option>
      <option value="000N95">Bruceton, TN </option>
      <option value="BA  76">Brunswick, MD</option>
      <option value="QD   6">Buffalo ICTF, NY</option>
      <option value="QDN 10">Buffalo Kenmore YD, NY</option>
      <option value="QDA  3">Buffalo Ohio Street, NY</option>
      <option value="QC 434">Buffalo, NY</option>
      <option value="S  626">Busch, FL</option>
      <option value="000426">Calera, AL</option>
      <option value="00J210">Calhoun, GA</option>
      <option value="00J240">Cartersville, GA</option>
      <option value="00H235">Casky Yard, KY</option>
      <option value="QR 110">Catskill, NY</option>
      <option value="S  362">Cayce, SC</option>
      <option value="0ZA141">Cayuga, IN </option>
      <option value="QM 214">Cecile, PQ</option>
      <option value="QVR  3">Cedar Hill, CT</option>
      <option value="SG 631">Cedartown, GA</option>
      <option value="SX 833">Central Florida ILC, FL</option>
      <option value="QJ  17">Chalk Point, MD</option>
      <option value="BAV 21">Chambersburg, PA </option>
      <option value="A  390">Charleston, SC</option>
      <option value="CA 454">Charleston, WV</option>
      <option value="SF 330">Charlotte, NC</option>
      <option value="CA 181">Charlottesville, VA</option>
      <option value="CEA 18">Chatham, ON</option>
      <option value="00J152">Chattanooga, TN</option>
      <option value="QBG  6">Chelsea, MA </option>
      <option value="DCQ 25">Chicago 59th St, IL </option>
      <option value="DC  27">Chicago Clearing, IL</option>
      <option value="QFX  1">Chicago Global One, IL </option>
      <option value="DD   2">Chicago, IL</option>
      <option value="BB  98">Chillicothe, OH</option>
      <option value="DAL 32">Cicero, IL </option>
      <option value="00T110">Cincinnati TOFC, OH </option>
      <option value="0KC110">Cincinnati, OH</option>
      <option value="BA 303">Clarksburg, WV</option>

      </select>

    SPLC            
    <select class="form-control styled-select" name="location" id="location_1" required title="(required)">
      <option value="">--select--</option>
        <option value="111111"> </option>
      <!--add split code numbers here-->    
    </select>

   Arrival Time:
    <input type="time" name="time" placeholder="hrs:mins" pattern="^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$" class="inputs time" > 


    Departure Time:         
    <input type="time" name="time" placeholder="hrs:mins" pattern="^([0-1]?[0-9]|2[0-4]):([0-5][0-9])(:[0-5][0-9])?$" class="inputs time" >
    </p>
</div>

<div class="Supervisor Name">  
    <p>
    <label for="division" class="col-lg-1 control-label">Supervisor Name: </label>
    <input id="LocationCity" type="text" value="" class="nobdrLtTopB1" placeholder="Supervisor Name" />
    ID Number:
    <input id="idnumber" type="text" value="" class="nobdrLtTopB1" placeholder="I.D. Number" />
    Phone Number:       
    <input id="phonenumber" type="text" value="" class="nobdrLtTopB1" placeholder="Phone Number" />
  </p>
    <p>MID Insp Name:
      <input id="midInspectorName" type="text" value="" class="nobdrLtTopB1" placeholder="MID Insp. Name" />
      Phone Number:         
      <input id="midphonenumber" type="text" value="" class="nobdrLtTopB1" placeholder="Phone Number" />
  </p>
</div>

<div>
 <div class="center"><ul>INSPECTION LOCATION</ul>

    <input type="checkbox" name="checkbox4" id="checkbox4">
    <label for="checkbox4"><i>CAR SHOP</i> 
      <input type="checkbox" name="checkbox5" id="checkbox5">
      <i>TRANSPORTATION YARD</i> 
      <input type="checkbox" name="checkbox6" id="checkbox6">
      <i>PROGRAM SHOP</i>
    </label>

  </div>
</div>

<h5>
# OF Cars Inspected <select class="form-control styled-select" name="carsInspected" id="carsInspected" >
        <option value="">--select--</option>
        <option value="1">  1</option>
        <option value="2"> 2</option>
        <option value="3"> 3</option>
        <option value="4"> 4</option>
        <option value="5"> 5</option>
        <option value="6"> 6</option>
        <option value="7"> 7</option>
        <option value="8"> 8</option>
        <option value="9"> 9</option>
        <option value="10"> 10</option>
                    </select>

<p># OF Car Exceptions <select class="form-control styled-select" name="exceptions" id="exceptions" >
      <option value="">--select--</option>
    <option value="1">  1</option>
    <option value="2"> 2</option>
    <option value="3"> 3</option>
    <option value="4"> 4</option>
    <option value="5"> 5</option>
    <option value="6"> 6</option>
    <option value="7"> 7</option>
    <option value="8"> 8</option>
    <option value="9"> 9</option>
    <option value="10"> 10</option>
    </select>
  </p>

<p>&nbsp;</p></h5>
       <p>&nbsp;</p>

       <h4>
        <table width="1277" border="5" cellspacing="4" cellpadding="4">
  <tbody>
    <tr>
      <th width="52" scope="col">LIST</th>
      <th width="1033" scope="col">DESCRIPTION OF DEFECT FINDINGS</th>
      <th width="112" scope="col">EXCECTION</th>
    </tr>
    <tr>
      <th scope="row">1</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_1" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e1" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">2</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_2" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e2" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">3</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_3" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e3" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">4</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_4" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e4" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">5</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_5" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e5" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">6</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_6" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e6" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">7</th>
     <td height="37" colspan="1">
                        <input id="ComWHS_7" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e7" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">8</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_8" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e8" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">9</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_9" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e9" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">10</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_10" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e10" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">11</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_11" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e11" name="cb_shop"/>
      </td>
    </tr>
    <tr>
      <th scope="row">12</th>
      <td height="37" colspan="1">
                        <input id="ComWHS_12" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_02" title="Com WHS 02" />
        </td>
      <td align="center">
         <input type="checkbox" id="cb_e12" name="cb_shop"/>
      </td>
    </tr>
  </tbody>
</table>
  </h4>


<table width="1277" border="5" cellspacing="4" cellpadding="4">
  <tbody>
    <tr>
      <th width="52" scope="col">LIST</th>
      <th width="1057" scope="col">PROVIDE ACTION PLAN FOR LISTED EXCEPTIONS ABOVE</th>

    </tr>
    <tr>
      <th scope="row">1</th>
      <td height="37" colspan="1"><input id="Com1_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">2</th>
      <td height="37" colspan="1"><input id="Com2_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">3</th>
      <td height="37" colspan="1"><input id="Com3_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">4</th>
      <td height="37" colspan="1"><input id="Com4_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">5</th>
      <td height="37" colspan="1"><input id="Com5_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">6</th>
      <td height="37" colspan="1"><input id="Com6_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">7</th>
      <td height="37" colspan="1"><input id="Com7_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">8</th>
      <td height="37" colspan="1"><input id="Com8_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">9</th>
      <td height="37" colspan="1"><input id="Com9_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">10</th>
      <td height="37" colspan="1"><input id="Com10_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">11</th>
      <td height="37" colspan="1"><input id="Com11_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
    <tr>
      <th scope="row">12</th>
      <td height="37" colspan="1"><input id="Com12_" type="text" value="" class="nobdrLtTopBl" placeholder="Comment on deficiency:" style="width:100%;" name="ComWHS_" title="Com WHS 02" /></td>

    </tr>
  </tbody>
</table>
</body>
</form>

</html>

here is the php file

<?php 
// Pear library includes
// You should have the pear lib installed
include_once('Mail.php');
include_once('Mail_Mime/mime.php');

//Settings 
$max_allowed_file_size = 1100; // size in KB 
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp");
$upload_folder = './uploads/'; //<-- this folder must be writeable by the script
$your_email = 'pasnthru99@gmail.com';//<<--  update this to your email address

$errors ='';

if(isset($_POST['submit']))
{
    //Get the uploaded file information
    $name_of_uploaded_file =  basename($_FILES['uploaded_file']['name']);

    //get the file extension of the file
    $type_of_uploaded_file = substr($name_of_uploaded_file, 
                            strrpos($name_of_uploaded_file, '.') + 1);

    $size_of_uploaded_file = $_FILES["uploaded_file"]["size"]/1024;

    ///------------Do Validations-------------
    if(empty($_POST['name'])||empty($_POST['email']))
    {
        $errors .= "\n Name and Email are required fields. ";   
    }
    if(IsInjected($visitor_email))
    {
        $errors .= "\n Bad email value!";
    }

    if($size_of_uploaded_file > $max_allowed_file_size ) 
    {
        $errors .= "\n Size of file should be less than $max_allowed_file_size";
    }

    //------ Validate the file extension -----
    $allowed_ext = false;
    for($i=0; $i<sizeof($allowed_extensions); $i++) 
    { 
        if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
        {
            $allowed_ext = true;        
        }
    }

    if(!$allowed_ext)
    {
        $errors .= "\n The uploaded file is not supported file type. ".
        " Only the following file types are supported: ".implode(',',$allowed_extensions);
    }

    //send the email 
    if(empty($errors))
    {
        //copy the temp. uploaded file to uploads folder
        $path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
        $tmp_path = $_FILES["uploaded_file"]["tmp_name"];

        if(is_uploaded_file($tmp_path))
        {
            if(!copy($tmp_path,$path_of_uploaded_file))
            {
                $errors .= '\n error while copying the uploaded file';
            }
        }

        //send the email
        $name = $_POST['name'];
        $visitor_email = $_POST['email'];
        $user_message = $_POST['message'];
        $to = $your_email;
        $subject="New form submission";
        $from = $your_email;
        $text = "A user  $name has sent you this message:\n $user_message";

        $message = new Mail_mime(); 
        $message->setTXTBody($text); 
        $message->addAttachment($path_of_uploaded_file);
        $body = $message->get();
        $extraheaders = array("From"=>$from, "Subject"=>$subject,"Reply-To"=>$visitor_email);
        $headers = $message->headers($extraheaders);
        $mail = Mail::factory("mail");
        $mail->send($to, $headers, $body);
        //redirect to 'thank-you page
        header('Location: thank-you.html');
    }
}

function IsInjected($str)
{
  $injections = array('(\n+)',
              '(\r+)',
              '(\t+)',
              '(%0A+)',
              '(%0D+)',
              '(%08+)',
              '(%09+)'
              );
  $inject = join('|', $injections);
  $inject = "/$inject/i";
  if(preg_match($inject,$str))
    {
    return true;
  }
  else
    {
    return false;
  }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>
<head>
    <title>File upload form</title>
<!-- define some style elements-->
<style>
label,a, body 
{
    font-family : Arial, Helvetica, sans-serif;
    font-size : 12px; 
}

</style>    

<?php
if(!empty($errors))
{
    echo nl2br($errors);
}
?>
<form method="POST" name="email_form_with_php" 
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" enctype="multipart/form-data"> 

0 个答案:

没有答案