SweetAlert + 表单 HTML + PHP

我很久以前就这样做过,但现在,我不记得了,我无法让它发挥作用......

所以,我有一个表单,其中包含对另一个 file.php 的操作,用于发送电子邮件,我想发出一个甜蜜的警报来告诉人们,好吧,您的消息已发送...

检查 CONTACT.PHP 中的代码

        <!-- main-container -->

        <div class="container main-container">

            <div class="col-md-6">

                <form action="envia.php" name="envia" id="envia" method="post">

                    <div class="row">

                        <div class="col-md-6">

                            <div class="input-contact">

                                <input type="text" name="nome" id="nome" required>

                                <span>Nome</span>

                            </div>

                        </div>

                        <div class="col-md-6">

                            <div class="input-contact">

                                <input type="text" id="email" name="email" required>

                                <span>Email</span>

                            </div>

                        </div>

                        <div class="col-md-12">

                            <div class="input-contact">

                                <input type="text" name="assunto" id="assunto" required>

                                <span>Assunto</span>

                            </div>

                        </div>

                        <div class="col-md-12">

                            <div class="textarea-contact">

                                <textarea name="menssagem" id="mensagem" required></textarea>

                                <span>Mensagem</span>

                            </div>

                        </div>

我已经尝试了很多方法,我只是重置一切,现在我不知道该怎么办......


收到一只叮咚
浏览 115回答 1
1回答

心有法竹

编辑为完整代码修复联系方式.php<!DOCTYPE html><html>&nbsp; <head>&nbsp; &nbsp; <meta charset="utf-8" />&nbsp; &nbsp; <meta http-equiv="X-UA-Compatible" content="IE=edge" />&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1" />&nbsp; &nbsp; <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->&nbsp; &nbsp; <title>Box personal portfolio Template</title>&nbsp; &nbsp; <!-- Bootstrap -->&nbsp; &nbsp; <link href="css/bootstrap.min.css" rel="stylesheet" />&nbsp; &nbsp; <link href="ionicons/css/ionicons.min.css" rel="stylesheet" />&nbsp; &nbsp; <link rel="icon" href="img/fav.png" type="image/x-icon" />&nbsp; &nbsp; <!-- main css -->&nbsp; &nbsp; <link href="css/style.css" rel="stylesheet" />&nbsp; &nbsp; <!-- Sweetalert -->&nbsp; &nbsp; <!-- <link href="sweet/style.css" type="text/css" rel="stylesheet" /> -->&nbsp; &nbsp; <link href="sweet/sweetalert.css" type="text/css" rel="stylesheet" />&nbsp; &nbsp; <script src="sweet/sweetalert.min.js" type="text/javascript"></script>&nbsp; &nbsp; <!-- modernizr -->&nbsp; &nbsp; <script src="js/modernizr.js"></script>&nbsp; &nbsp; <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->&nbsp; &nbsp; <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->&nbsp; &nbsp; <!--[if lt IE 9]>&nbsp; &nbsp; &nbsp; <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>&nbsp; &nbsp; &nbsp; <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>&nbsp; &nbsp; <![endif]-->&nbsp; </head>&nbsp; <body>&nbsp; &nbsp; <!-- Preloader -->&nbsp; &nbsp; <div id="preloader">&nbsp; &nbsp; &nbsp; <div class="pre-container">&nbsp; &nbsp; &nbsp; &nbsp; <div class="spinner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="double-bounce1"></div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="double-bounce2"></div>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- end Preloader -->&nbsp; &nbsp; <div class="container-fluid">&nbsp; &nbsp; &nbsp; <!-- box-header -->&nbsp; &nbsp; &nbsp; <header class="box-header">&nbsp; &nbsp; &nbsp; &nbsp; <div class="box-logo">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="index.html"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ><img src="img/logo.png" width="80" alt="Logo"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; /></a>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <!-- box-nav -->&nbsp; &nbsp; &nbsp; &nbsp; <a class="box-primary-nav-trigger" href="#0">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="box-menu-text">Menu</span&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ><span class="box-menu-icon"></span>&nbsp; &nbsp; &nbsp; &nbsp; </a>&nbsp; &nbsp; &nbsp; &nbsp; <!-- box-primary-nav-trigger -->&nbsp; &nbsp; &nbsp; </header>&nbsp; &nbsp; &nbsp; <!-- end box-header -->&nbsp; &nbsp; &nbsp; <!-- nav -->&nbsp; &nbsp; &nbsp; <nav>&nbsp; &nbsp; &nbsp; &nbsp; <ul class="box-primary-nav">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-label">About me</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="index.html">Intro</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="about.html">About me</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="services.html">services</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li><a href="portfolio.html">portfolio</a></li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="contact.html">contact me</a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <i class="ion-ios-circle-filled color"></i>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-label">Follow me</li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-social">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#0"><i class="ion-social-facebook"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-social">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#0"><i class="ion-social-instagram-outline"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-social">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#0"><i class="ion-social-twitter"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <li class="box-social">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <a href="#0"><i class="ion-social-dribbble"></i></a>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </li>&nbsp; &nbsp; &nbsp; &nbsp; </ul>&nbsp; &nbsp; &nbsp; </nav>&nbsp; &nbsp; &nbsp; <!-- end nav -->&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- top bar -->&nbsp; &nbsp; <div class="top-bar">&nbsp; &nbsp; &nbsp; <h1>contact Me</h1>&nbsp; &nbsp; &nbsp; <p><a href="#">Home</a> / Contact Me</p>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- end top bar -->&nbsp; &nbsp; <!-- main-container -->&nbsp; &nbsp; <div class="container main-container">&nbsp; &nbsp; &nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; <form name="envia" action="#" id="envia">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="row">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="input-contact">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="nome" id="nome" required />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Nome</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="input-contact">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" id="email" name="email" required />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Email</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="input-contact">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="assunto" id="assunto" required />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Assunto</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="textarea-contact">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <textarea name="menssagem" id="mensagem" required></textarea>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Mensagem</span>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <div class="col-md-12">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id="submit"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; type="submit"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name="enviar"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; value="enviar"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; class="btn btn-box"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; </form>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <div class="col-md-6">&nbsp; &nbsp; &nbsp; &nbsp; <h3 class="text-uppercase">Entre em contacto</h3>&nbsp; &nbsp; &nbsp; &nbsp; <h5>Pedidos orçamento e esclarecimento de dúvidas.</h5>&nbsp; &nbsp; &nbsp; &nbsp; <div class="h-30"></div>&nbsp; &nbsp; &nbsp; &nbsp; <p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />Poderá usar este formulário para esclarecimento de dúvidas ou&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pedido de orçamento, para pedidos de orçamento deverá indicar o mais&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; claro possivel o que pretende.&nbsp; &nbsp; &nbsp; &nbsp; </p>&nbsp; &nbsp; &nbsp; &nbsp; <div class="contact-info">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><i class="ion-android-call"></i>911729199</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p><i class="ion-ios-email"></i>info@obruno.pt</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </div>&nbsp; &nbsp; <!-- end main-container -->&nbsp; &nbsp; <!-- footer -->&nbsp; &nbsp; <footer>&nbsp; &nbsp; &nbsp; <div class="container-fluid">&nbsp; &nbsp; &nbsp; &nbsp; <p class="copyright">© Box Portfolio 2016</p>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </footer>&nbsp; &nbsp; <!-- end footer -->&nbsp; &nbsp; <!-- back to top -->&nbsp; &nbsp; <a href="#0" class="cd-top"><i class="ion-android-arrow-up"></i></a>&nbsp; &nbsp; <!-- end back to top -->&nbsp; &nbsp; <!-- jQuery -->&nbsp; &nbsp; <script src="js/jquery-2.1.1.js"></script>&nbsp; &nbsp; <!--&nbsp; plugins -->&nbsp; &nbsp; <script src="js/bootstrap.min.js"></script>&nbsp; &nbsp; <script src="js/menu.js"></script>&nbsp; &nbsp; <script src="js/animated-headline.js"></script>&nbsp; &nbsp; <script src="js/isotope.pkgd.min.js"></script>&nbsp; &nbsp; <!--&nbsp; custom script -->&nbsp; &nbsp; <script src="js/custom.js"></script>&nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; // This will prevent the default button action and the page won't refresh&nbsp; &nbsp; &nbsp; document.querySelector("#submit").addEventListener(&nbsp; &nbsp; &nbsp; &nbsp; "click",&nbsp; &nbsp; &nbsp; &nbsp; function (event) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; process();&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; false&nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; // This will make a post request to your php file&nbsp; &nbsp; &nbsp; function process() {&nbsp; &nbsp; &nbsp; &nbsp; // Getting the inputs&nbsp; &nbsp; &nbsp; &nbsp; var message = document.getElementById("mensagem").value;&nbsp; &nbsp; &nbsp; &nbsp; var email = document.getElementById("email").value;&nbsp; &nbsp; &nbsp; &nbsp; var subject = document.getElementById("assunto").value;&nbsp; &nbsp; &nbsp; &nbsp; var name = document.getElementById("nome").value;&nbsp; &nbsp; &nbsp; &nbsp; // Some data validation to see if the user have filled the required fields&nbsp; &nbsp; &nbsp; &nbsp; if (message && email && subject && name) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Setting the AJAX request handler&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var xmlhttp = new XMLHttpRequest();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Waiting for a response&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp.onreadystatechange = function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // If the response is 200 aka "OK"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (this.readyState == 4 && this.status == 200) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Show your success alert here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; swal({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; title: "Message Sent!",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text: "Thank you for contacting us.",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timer: 2000,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; showConfirmButton: false,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById("envia").reset();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Setting the request type and data to be sent&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp.open("POST", "envia.php", true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp.setRequestHeader(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "Content-type",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "application/x-www-form-urlencoded"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlhttp.send(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "nome=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&email=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&assunto=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subject +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "&mensagem=" +&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; message&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; );&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Alert the user to fill the required fields&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("please fill all required fields");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; </script>&nbsp; </body></html>发送.php<?php$nomeremetente&nbsp; &nbsp; &nbsp;= $_POST['nome'];$emailremetente&nbsp; = $_POST['email'];$emaildestinatario&nbsp; = 'info@mydomain.pt';$assunto = $_POST['assunto'];$mensagem = $_POST['mensagem'];$content = '<strong>Formulário de Contato</strong><p><b>Nome:</b> ' . $nomeremetente . ' <p><b>E-Mail:</b> ' . $emailremetente . ' <p><b>Assunto:</b> ' . $assunto . ' <p><b>Mensagem:</b> ' . $mensagem . '</p><hr>';$headers = array(&nbsp; &nbsp; 'From' => $emailremetente,&nbsp; &nbsp; 'Reply-To' => $emaildestinatario,&nbsp; &nbsp; 'MIME-Version' => '1.1',&nbsp; &nbsp; 'Content-type' => 'text/html;charset=UTF-8',&nbsp; &nbsp; 'X-Mailer' => 'PHP/' . phpversion());if (isset($nomeremetente) && isset($emailremetente) && isset($emaildestinatario) && isset($assunto) && isset($mensagem) && isset($content)) {&nbsp; &nbsp; mail($emaildestinatario, $assunto, $content, $headers);&nbsp; &nbsp; echo "SUCCESS";&nbsp; &nbsp; http_response_code(200);} else {&nbsp; &nbsp; echo "FAILED";&nbsp; &nbsp; http_response_code(404);}请注意,我已经添加了甜蜜的警报,您可以继续并自定义电子邮件成功后显示的消息注意:不要忘记将您要接收电子邮件的电子邮件更改为您域中的电子邮件,否则电子邮件最终将无法发送或进入垃圾邮件
打开App,查看更多内容
随时随地看视频慕课网APP