HTML表单提交方法:文件上传和电子邮件 [英] HTML form submit how to: File Upload and email

查看:77
本文介绍了HTML表单提交方法:文件上传和电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML表单,其中包含几个不同的字段和下拉菜单.该表单具有几个不同的文本字段,几个下拉菜单和一个文件上传.表单提交后,会通过电子邮件发送给我.以下是HTML和PHP提交.

I have an HTML form that have several different fields and drop down menus. The form has several different text fields, a couple drop down menus, and a file upload. Once the form is submitted it is sent to me via email. Below is the HTML and the PHP submit.

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

I can't seem to figure out how to get the file to upload and then be sent to me as an email attachment. I would appreciate any help. Thanks

html看起来像这样`

The html looks like this `

            <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 -->

推荐答案

Amm ...在您的代码中看不到"标记.当您想通过这种形式传递数据时,您必须拥有它,当前,您只有一个不错的表单用户界面,但实际上并不起作用.

Amm... I don't see "" tag in your code. You must have it when you want to pass data with this form, at the current you just have a nice UI of a form but it doesn't actually work.

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

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

Please notice that the "enctype" attribute is needed for uploading files, if you just want to send simple inputs you don't need it.

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

If you just cut those lines off , there are 2 options. 1. Send a URL of the file in the email content. (Easiest) 2. Add the file as an attachment (Check out this code: http://www.texelate.co.uk/blog/send-email-attachment-with-php/)

顺便说一句,而不是具有检查"isset"是否存在的巨大条件,为什么不用字段名和一个循环来写一个数组,该数组检查每个和一个他们吗?

BTW, instead of having this HUGE condition of checking if "isset", why not writing an array with the field's names and a loop which check each and one of them?

这篇关于HTML表单提交方法:文件上传和电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆