简体   繁体   English

如何在表单提交时发送电子邮件并在数据库中保存数据

[英]How to send email while form submission with saving data in database

Currently, our form is saving data in database on form submission but now we want (for example: to send email to email@example.com) as well. 当前,我们的表单正在提交表单时将数据保存在数据库中,但是现在我们也想要(例如:将电子邮件发送到email@example.com)。 Below is the code but we are not sure, which script and where exactly we need to add. 下面是代码,但我们不确定,我们需要在哪个脚本中以及确切的位置添加。

              <!-- Nav tabs -->
              <ul class="nav nav-tabs" role="tablist" id="myTabs">
                <li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Project Brief Form</a></li>
                <li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Contact Form</a></li>
              </ul>
              <!-- Tab panes -->
              <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="home">
                    <form  action="#" method="post" enctype="multipart/form-data" id="project">
                        <div class="row">
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <label>Tell us about your project in your own words? What is, for you, the main goal of the project? *</label>
                                    <textarea class="form-control" rows="4" name="project" required></textarea>
                                </div>
                            </div>
                            <div class="col-sm-6">
                              <div class="form-group">
                                <label>Full name</label>
                                <input type="text" class="form-control" placeholder="Full name" name="name" required>
                              </div>
                              <div class="form-group">
                                <label>Email / Skypename</label>
                                <input type="text" class="form-control" placeholder="Email / Skypename" name="email" required>
                              </div>
                              <div class="form-group">
                                <label>Phone</label>
                                <input type="number" class="form-control" placeholder="Phone" name="phone" required>
                              </div>
                            </div>
                            <div class="col-sm-6">
                              <div class="form-group">
                                <label>How much do you want to spend?</label>
                                <select class="form-control" name="spend" required>
                                  <option value="500 - 3000 $">500 - 3000 $</option>
                                  <option value="3000 - 10000 $">3000 - 10000 $</option>
                                  <option value="10000 - 50000 $">10000 - 50000 $</option>
                                  <option value="50000 - 250000 $">50000 - 250000 $</option>
                                  <option value="250000 $ +">250000 $ +</option>
                                </select>
                              </div>
                              <div class="form-group">
                                <label>Which services are you interested in?</label>
                                <input type="text" class="form-control" placeholder="Which services are you interested in?" name="service" required>
                              </div>
                              <div class="form-group">
                                <label>How did you find out about us?</label>
                                <input type="text" class="form-control" placeholder="Example: Google, Facebook, Behance..." name="about" required>
                              </div>
                            </div>
                            <div class="col-sm-12 text-center">
                                <button type="submit" id="btnSubmit" onclick="SubmitForm('project','dubai','add_project')" class="btn btn-default">Submit</button>
                          </div>
                          <div id="result"></div>
                        </div>
                    </form>
                </div>
                <div role="tabpanel" class="tab-pane" id="profile">
                    <form  action="#" method="post" enctype="multipart/form-data" id="contact">
                        <div class="row">
                            <div class="col-sm-12">
                              <div class="form-group">
                                <label>Full name</label>
                                <input type="text" class="form-control" placeholder="Full name" name="name" required>
                              </div>
                              <div class="form-group">
                                <label>Email</label>
                                <input type="email" class="form-control" placeholder="Email" name="email" required>
                              </div>
                              <div class="form-group">
                                <label>Phone number</label>
                                <input type="number" class="form-control" placeholder="Phone number" name="phone" required>
                              </div>
                            </div>
                            <div class="col-sm-12">
                                <div class="form-group">
                                    <label>Tell us about your project in your own words? What is, for you, the main goal of the project? *</label>
                                    <textarea class="form-control" onclick="SubmitForm('contact','skyhigh','add_contact')" rows="4" name="about"></textarea>
                                </div>
                            </div>
                            <div class="col-sm-12 text-center">
                                <button type="submit" class="btn btn-default">Submit</button>
                            </div>
                            <div id="massage"></div>
                        </div>
                    </form>
                </div>
              </div>
            </div>

            <script type="text/javascript">
                $('#myTabs a').click(function (e) {
                  e.preventDefault()
                  $(this).tab('show');
                });
            </script>
        </div>
    </div>
</div>

              <script type="text/javascript">
function SubmitForm(FormID, Controller, FunctionName) {
           $("#"+FormID).unbind('submit').submit(function (event) {
               var formData = $( this ).serialize();
               $.ajax({
                        type: 'POST', 
                        url: "<?php echo $this->config->base_url(); ?>" + Controller + "/"+ FunctionName,
                        data: formData
                    })
                    .done(function (data) {
                        if(FormID=="project")
                        {
                        // $("#"+ ResultDivId).html(data);
                          $('<center><div class="alert alert-success success-order"><h4>Your Data Send Successfully, We will Contact You Soon.</h4></div></center>').insertBefore('#result').delay(10000).fadeOut();
                        }
                        else
                        {
                          $('<center><div class="alert alert-success success-order"><h4>Your Data Send Successfully, We will Contact You Soon.</h4></div></center>').insertBefore('#massage').delay(10000).fadeOut();
                        }   
                    });

                    event.preventDefault();
                    $("#"+FormID)[0].reset();
                });
        }
</script>

We want to send emails while form submission as its saving data in database only. 我们只想在表单提交时发送电子邮件,因为它仅将数据保存在数据库中。

You should add code for email sending right where you handling a form data and saving it to database. 您应该在处理表单数据并将其保存到数据库的位置添加用于电子邮件发送的代码。 To send emails you'll need a properly configured server environment. 要发送电子邮件,您需要正确配置的服务器环境。 It's a big topic and you haven't provided any information about your server OS. 这是一个大话题,您尚未提供有关服务器操作系统的任何信息。 Anyway, you can just try 无论如何,您可以尝试

sudo apt-get install mailutils

on your server. 在您的服务器上。 I am just assuming you're running Ubuntu. 我只是假设您正在运行Ubuntu。 Then you'll have to use Swiftmailer package and configure it to use, for example, google's smtp. 然后,您将不得不使用Swiftmailer软件包并将其配置为使用例如Google的smtp。 Lastly, you'll need to set-up a google account to use for sending e-mails and configure it with correct permissions to use in your app. 最后,您需要设置一个Google帐户来发送电子邮件,并为其配置正确的权限以在您的应用中使用。 Like I said, it's a big topic, a lot of things must be done to achieve what you want, and your question is very general and unclear. 就像我说的,这是一个很大的话题,要实现您想要的目标,必须做很多事情,而且您的问题非常笼统且不清楚。 Alternatively, you can look at things like mailgun for simplicity. 另外,为了简单起见,您可以查看诸如mailgun之类的东西。

For this you will need a mailing system. 为此,您将需要一个mailing系统。 On www.packagist.org you can find a lot of very good working composer installing projects. 在www.packagist.org上,您可以找到许多非常好的工作作曲家来安装项目。

For example: https://packagist.org/packages/swiftmailer/swiftmailer 例如: https : //packagist.org/packages/swiftmailer/swiftmailer

Please read the documentation on how to install and use them. 请阅读有关如何安装和使用它们的文档。

Composer can be found here incase you are not using it yet: https://getcomposer.org/ 如果您尚未使用Composer,可以在这里找到: https : //getcomposer.org/

To send an email: 发送电子邮件:

The mailing system is basically being called near the moment you save your data into the database. 在您将数据保存到数据库的那一刻,基本上就调用了邮件系统。

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

相关问题 如何只向上次提交的谷歌表单发送电子邮件? - How to only send email to the last google form submission? 在需要防止由于socket.io提交表单时如何将表单数据保存到数据库? - How to save form data to database while needing to prevent form submission due to socket.io? 如何将数据从表单发送到 email - How to send data from a form to a email 在提交表单时,隐藏表单女主角并使用jQuery发送电子邮件 - On form submission hide the form diva and send an email using jQuery 反应-有条件不在表单提交中发送数据密钥 - React - conditionally not send data key in form submission 用javascript组成一个数组,然后将该数组发送到php以提交到数据库 - form an array with javascript and then send the array to php for submission to database 如何使用Angularjs和PHP发送带有表单数据和附件的电子邮件 - How to Send Email With Form data and Attachment Using Angularjs and PHP 如何在表单提交上加载extjs 4网格数据? - How to load extjs 4 grid data on form submission? 表单提交后如何查看邮箱已经存在 - How to check email already exists after form submission 如何使用jquery / javascript &amp;&amp;重置html中的表单数据,以及如何使用jquery将表单数据发送到电子邮件? - How to reset the form data in html using jquery/javascript && and also how to send the form data to an email using jquery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM