简体   繁体   English

提交联系表格数据

[英]Submitting contact form data

The contact form has several text fields and several dropdowns with the ability to select multiple values.联系表单有几个文本字段和几个下拉菜单,可以选择多个值。 The question is how to send these values by e-mail?问题是如何通过电子邮件发送这些值? Text values of type string (one word at a time) are sent fine.字符串类型的文本值(一次一个单词)可以正常发送。 But how to send arrays containing multiple values?但是如何发送包含多个值的数组呢? How to collect them using the $_POST method and put them in a letter?如何使用 $_POST 方法收集它们并将它们放入信中?

Form:形式:

<section class="post-content-area pt-20">
    <div class="container">
        <div class="row">
            <div class="col-lg-8 posts-list">
                <div class="card card-signin my-5">
                    <div class="card-body">
                        <h5 class="card-title text-center" style="font-size: 26px">Test contact form</h5>
                        <form method="post" action="email-script.php" enctype="multipart/form-data" id="emailForm"> 
                            <div class="form-group">
                                <input type="text" name="name" id="name" class="form-control" placeholder="Name" >
                                <div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
                            </div>
                            <div class="form-group">
                                <input type="text" name="surname" id="surname" class="form-control" placeholder="Surame" >
                                <div id="nameError" style="color: red;font-size: 14px;display: none">nameError</div>
                            </div>
                            <div class="form-group">
                                <input type="text" name="phone" id="phone" class="form-control"  placeholder="Phone" >
                                <div id="subjectError" style="color: red;font-size: 14px;display: none">subjectError</div>
                            </div>
    <div class="form-group">
     <label>First Level Category</label><br />
     <select id="first_level" name="first_level[]" multiple class="form-control">
     <?php
     foreach($result as $row)
     {
      echo '<option value="'.$row["first_level_category_id"].'">'.$row["first_level_category_name"].'</option>';
     }
     ?>
     </select>
    </div>
    <div class="form-group">
     <label>Second Level Category</label><br />
     <select id="second_level" name="second_level[]" multiple class="form-control">
 
     </select>
    </div>
    <div class="form-group">
     <label>Third Level Category</label><br />
     <select id="third_level" name="third_level[]" multiple class="form-control">
 
     </select>
    </div>
        <div class="form-group">
                                <input type="file" name="attachment" id="attachment" class="form-control">
                                <div id="attachmentError" style="color: red;font-size: 14px;display: none">attachmentError</div>
                            </div>
                            <div class="submit">
                                <center><input type="submit" name="submit" onclick="return validateEmailSendForm();" class="btn btn-success" value="SUBMIT"></center>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>

Emailing script: https://pastebin.com/3SR67MUP电子邮件脚本: https ://pastebin.com/3SR67MUP

There are multiple ways on how to send them via email.如何通过电子邮件发送它们有多种方法。

The easiest way is getting them on your form action page and sending them with the php mail() function.最简单的方法是将它们放在您的表单操作页面上并使用 php mail() 函数发送它们。

To make the email look better you can create an HTML email template file.为了使电子邮件看起来更好,您可以创建一个 HTML 电子邮件模板文件。

Then get the file data with file_get_contents() and resetting strings with your collected data with the str_replace function and putting the data into your mail funtion.然后使用 file_get_contents() 获取文件数据,并使用 str_replace 函数使用收集的数据重置字符串,并将数据放入您的邮件函数中。

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM