HTML表单提交如何:文件上传和电子邮件

时间:2011-11-20 00:32:07

标签: html forms email file-upload submit

我有一个HTML表单,它有几个不同的字段和下拉菜单。表单有几个不同的文本字段,几个下拉菜单和一个文件上传。表单提交后,将通过电子邮件发送给我。下面是HTML和PHP提交。

我似乎无法弄清楚如何将文件上传,然后作为电子邮件附件发送给我。我将不胜感激任何帮助。感谢

html看起来像这样 `

            <label class="label" for="name">Name:</label> <input id="name" class="name" type="text" name="distributorName" /> 

            <label class="label" for="distributorId">Distributor ID:</label> <input id="name" class="name" type="text" name="distributorId" /> 

            <label class="label" for="email">Distributor Email:</label> <input id="email" class="name" type="text" name="email" />
        <h2>Business Information</h2>
            <label class="label" for="businessName">Business Name:</label> 
                <input id="name" class="name" type="text" name="businessName" /> 
            <label class="label" for="phone">Business Phone Number:</label> 
                <input id="phone" class="name" type="text" name="phone" /> 
            <label class="label" for="street">Address:</label> 
                <input id="street" class="name" type="text" name="street" /> 
            <label class="label" for="city">City:</label> <input id="city" class="name" type="text" name="city" />

        <div id="state">
            <label id="state" class="label" for="state">State:</label> 
                <select class="dropdown" name="state"> 
                <option value="AL">AL</option> 
                <option value="AK">AK</option>
                <option value="AZ">AZ</option> 
                <option value="AR">AR</option>
                <option value="CA">CA</option> 
                <option value="CO">CO</option>
                <option value="CT">CT</option> 
                <option value="DE">DE</option>
                <option value="FL">FL</option> 
                <option value="GA">GA</option>
                <option value="HI">HI</option> 
                <option value="ID">ID</option>
                <option value="IL">IL</option> 
                <option value="IN">IN</option>
                <option value="IA">IA</option> 
                <option value="KS">KS</option>
                <option value="KY">KY</option> 
                <option value="LA">LA</option>
                <option value="ME">ME</option> 
                <option value="MD">MD</option>
                <option value="MA">MA</option> 
                <option value="MI">MI</option>
                <option value="MN">MN</option> 
                <option value="MS">MS</option>
                <option value="MO">MO</option> 
                <option value="MT">MT</option>
                <option value="NE">NE</option> 
                <option value="NV">NV</option>
                <option value="NH">NH</option> 
                <option value="NJ">NJ</option>
                <option value="NM">NM</option> 
                <option value="NY">NY</option>
                <option value="NC">NC</option> 
                <option value="ND">ND</option>
                <option value="OH">OH</option> 
                <option value="OK">OK</option>
                <option value="OR">OR</option> 
                <option value="PA">PA</option>
                <option value="RI">RI</option> 
                <option value="SC">SC</option>
                <option value="SD">SD</option> 
                <option value="TN">TN</option>
                <option value="TX">TX</option> 
                <option value="UT">UT</option>
                <option value="VT">VT</option> 
                <option value="VA">VA</option>
                <option value="WA">WA</option> 
                <option value="WV">WV</option>
                <option value="WI">WI</option> 
                <option value="WY">WY</option>
            </select>
        </div>
        <div id="zip">
            <label id="zip" class="label" for="zip">ZIP Code:</label> 
                <input id="zip" class="name" type="text" name="zip" />
        </div>

            <label class="nobg" for="uploaded_file">Company Logo:</label> 
                <input class="nobg" type="file" name="uploaded_file" />
    </div>

    <div id="right">
       <div style="width:50%; float:left;">
        <label class="label" for="validOthers">Valid with other Coupons:</label> 
            <select class="dropdown" name="validOthers"> 
                <option value="yes">Yes</option> 
                <option value="no">No</option> 
            </select>

        <label class="label" for="oneCoupon">One Coupon Per Customer:</label> 
            <select class="dropdown" name="oneCoupon"> 
                <option value="yes">Yes</option> 
                    <option value="no">No</option> </select>
       </div>
       <div style="width:50%; float:left;">
        <label class="label" for="outOfarea">Out of Area Coupon:</label> 
            <select class="dropdown" name="outOfarea"> 
                <option value="yes">Yes</option> 
                    <option value="no">No</option> </select>

            <label class="label" for="businessCategory">Business Category (Optional):</label> 
                <input style="width:200px;" type="text" name="businessCategory" /> 
       </div>


        <h2>Offer 1</h2>
            <label class="label" for="offer1">Your Offer:</label> 
                <input type="text"class="offers" name="offer1" /> 
            <label class="label" for="offer1Days">Number of days the offer will last for:</label> 
                <input class="days" type="text" name="offer1Days" /> 
            <label class="label" for="offer1Quantity">Number of Coupons:</label> 
                <input class="qty" type="text" name="offer1Quantity" />

        <h2>Offer 2</h2>
            <label class="label" for="offer2">Your Offer:</label> 
                <input type="text" class="offers" name="offer2" /> 
            <label class="label" for="offer2Days">Number of days the offer will last for:</label> 
                <input class="days" type="text" name="offer2Days" /> 
            <label class="label" for="offer2Quantity">Number of Coupons:</label> 
                <input class="qty" type="text" name="offer2Quantity" />
        <h2>Offer 3</h2>
            <label class="label" for="offer3">Your Offer:</label> 
                <input type="text" class="offers" name="offer3" /> 
            <label class="label" for="offer3Days">Number of days the offer will last for:</label> 
                <input class="days" type="text" name="offer3Days" /> 
            <label class="label" for="offer3Quantity">Number of Coupons:</label> 
                <input class="qty" type="text" name="offer3Quantity" /> 

<input id="order" class="submit" type="submit" value="Submit" />

`

这是PHP提交

if(isset($_POST['email'])) {

    $email_to = "joel@joelshawdesigns.com";
    $email_subject = "test form Submission";


    function died($error) {
        // your error code can go here
        echo "We are very sorry, but there were error(s) found with the form you submitted. ";
        echo "These errors appear below.<br /><br />";
        echo $error."<br /><br />";
        echo "Please go back and fix these errors.<br /><br />";
        die();
    }

    if(!isset($_POST['distributorName']) ||
        !isset($_POST['distributorId']) ||    
        !isset($_POST['email']) ||
        !isset($_POST['businessName']) ||
        !isset($_POST['phone']) ||                        
        !isset($_POST['street']) ||                        
        !isset($_POST['city']) ||                        
        !isset($_POST['state']) ||                        
        !isset($_POST['zip']) ||                        
        !isset($_POST['validOthers']) ||                        
        !isset($_POST['oneCoupon']) ||
        !isset($_POST['outOfarea']) ||                        
        !isset($_POST['businessCategory']) ||                                
        !isset($_POST['offer1']) ||                        
        !isset($_POST['offer1Days']) ||                        
        !isset($_POST['offer1Quantity']) ||                        
        !isset($_POST['offer2']) ||                        
        !isset($_POST['offer2Days']) ||                        
        !isset($_POST['offer2Quantity']) ||                        
        !isset($_POST['offer3']) ||                        
        !isset($_POST['offer3Days']) ||                        
        !isset($_POST['offer3Quantity']
        )) {
        died('We are sorry, but there appears to be a problem with the form you submitted.');       
    }

    $distributorName = $_POST['distributorName']; // not required
    $distributorId = $_POST['distributorId']; // not required
    $email_from = $_POST['email']; // not required
    $businessName = $_POST['businessName']; // not required
    $phone = $_POST['phone']; // not required
    $street = $_POST['street']; // not required
    $city = $_POST['city']; // not required    
    $state = $_POST['state']; // not required
    $zip = $_POST['zip']; // not required
    $validOthers = $_POST['validOthers']; // not required
    $oneCoupon = $_POST['oneCoupon']; // not required
    $outOfarea = $_POST['outOfarea']; // not required
    $businessCategory = $_POST['businessCategory']; // not required    
    $offer1 = $_POST['offer1']; // not required
    $offer1Days = $_POST['offer1Days']; // not required
    $offer1Quantity = $_POST['offer1Quantity']; // not required
    $offer2 = $_POST['offer2']; // not required
    $offer2Days = $_POST['offer2Days']; // not required
    $offer2Quantity = $_POST['offer2Quantity']; // not required
    $offer3 = $_POST['offer3']; // not required
    $offer3Days = $_POST['offer3Days']; // not required
    $offer3Quantity = $_POST['offer3Quantity']; // not required

    $error_message = "";
    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
  if(!preg_match($email_exp,$email_from)) {
    $error_message .= 'Hmm...The Email Address you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[A-Za-z .'-]+$/";
  if(!preg_match($string_exp,$distributorName)) {
    $error_message .= 'Hmm...The name you entered does not appear to be valid.<br />';
  }
  if(!preg_match($string_exp,$businessName)) {
    $error_message .= 'The Business Name you entered does not appear to be valid.<br />';
  }
    $string_exp = "/^[0-9.'-]+$/";  
  if(!preg_match($string_exp,$phone)) {
    $error_message .= 'The Phone Number you entered does not appear to be valid.<br />';
  }    

  if(strlen($error_message) > 0) {
    died($error_message);
  }
    $email_message = "Order From: $distributorName\n\n";


    function clean_string($string) {
      $bad = array("content-type","bcc:","to:","cc:","href");
      return str_replace($bad,"",$string);
    }

    $email_message .= "Distributor Name:  ".clean_string($distributorName)."\n";
    $email_message .= "Distributor ID:  ".clean_string($distributorId)."\n";
    $email_message .= "Distributor Email:  ".clean_string($email_from)."\n";
    $email_message .= "Business Name:  ".clean_string($businessName)."\n";
    $email_message .= "Business Telephone:  ".clean_string($phone)."\n";
    $email_message .= "Business Street:  ".clean_string($street)."\n";
    $email_message .= "Business City:  ".clean_string($city)."\n";
    $email_message .= "Business State:  ".clean_string($state)."\n";
    $email_message .= "Business Zip:  ".clean_string($zip)."\n"; 
    $email_message .= "Valid with other coupons:  ".clean_string($validOthers)."\n";
    $email_message .= "One coupon per customer:  ".clean_string($oneCoupon)."\n";
    $email_message .= "Out of Area Coupon:  ".clean_string($outOfarea)."\n";
    $email_message .= "Business Category:  ".clean_string($businessCategory)."\n";    
    $email_message .= "Offer 1:  ".clean_string($offer1)."\n";
    $email_message .= "Offer 1 Duration:  ".clean_string($offer1Days)."\n";
    $email_message .= "Offer 1 Quantity:  ".clean_string($offer1Quantity)."\n";
    $email_message .= "Offer 2:  ".clean_string($offer2)."\n";
    $email_message .= "Offer 2 Duration:  ".clean_string($offer2Days)."\n";
    $email_message .= "Offer 2 Quantity:  ".clean_string($offer2Quantity)."\n";
    $email_message .= "Offer 3: ".clean_string($offer3)."\n";
    $email_message .= "Offer 3 Duration:  ".clean_string($offer3Days)."\n";
    $email_message .= "Offer 3 Quantity:  ".clean_string($offer3Quantity)."\n";

if ( move_uploaded_file ($_FILES['uploaded_file'] ['tmp_name'], 
       "uploads/{$_FILES['uploadFile'] ['name']}")  )
      {  print '<p> The file has been successfully uploaded </p>';
       }     

$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);  
?>
<!-- include your own success html here -->

1 个答案:

答案 0 :(得分:0)

嗯......我的代码中没有看到“”标签。 当您想要使用此表单传递数据时,您必须拥有它, 在目前你只有一个很好的用户界面,但它实际上没有用。

<form action="your_php_file.php" method="post" enctype="multipart/form-data">

请注意,上传文件需要“enctype”属性, 如果您只想发送简单的输入,则不需要它。


如果您只是关闭这些线,有2个选项。 1.在电子邮件内容中发送文件的URL。 (最简单的) 2.将文件添加为附件(请查看此代码:http://www.texelate.co.uk/blog/send-email-attachment-with-php/

顺便说一句, 而不是有这个巨大的条件检查是否“isset”, 为什么不写一个带有字段名称的数组和一个检查每个和一个的循环 它们?