javascript - 通过表单发送电子邮件,无需刷新

标签 javascript php jquery html

我正在创建带有视差滚动的页面,联系人框位于最后一部分。如果我使用简单的 html + php 联系人框,页面将刷新到第一部分,因此用户必须滚动回联系人部分才能查看结果。为了纠正这个问题,我正在尝试制作无需重新加载页面即可发送电子邮件的脚本。 (通过 Jquery)

它是这样的:

HTML:

<form method="post" enctype="multipart/form-data" id="formularz">
        <label  for="name">Imię</label><input type="text" name="name" id="formularz_name"/></br></br>
        <label  for="email">Email</label><input type="text" name="email" id="formularz_email"/></br></br>
        <label  for="phone">Telefon</label><input type="text" name="phone" id="formularz_phone"/></br></br>
        <label  for="enquiry">Zapytanie</label><textarea name="enquiry" cols="20" rows="10" id="formularz_text"></textarea></br></br>
        <input type="submit" id="contact_button" value="Wyślij" />
        </form>

Jquery:

$('#contact_button').click(function () {


    var name = $('#formularz_name').val();
    var email = $('#formularz_email').val();
    var phone = $('#formularz_phone').val();
    var text = $('#formularz_text').val();

          $.ajax({
            url: 'inc/contact.php',
            data: {'name': name, 'email': email, 'phone': phone, 'text': text},
            type: 'POST',
            success: function(odp){
                alert(odp);
            }

           });

});

PHP:

<?php





    if (!$name || !$email || !$phone || !$text) {
        $problem = TRUE;
       echo("<br><p>Musisz wypełnić wszystkie pola.</p>");
    }       

    if (!$problem){


    $data = date("d.m.y");


    $message = "
    <p>Name: $name</p>
    <p>Phone: $phone</p>
    <p>Email: $email</p>
    <br>
    <p>Enquiry: $text</p>";


$od = "$email";
$content = $message;
$header = "From: $od \r\n";
$header  .= 'MIME-Version: 1.0' . "\r\n";
$header .= 'Content-type: text/html; charset=UTF-8' . "\r\n";
 (mail('lethysek@gmail.com', 'New message from website Angelzena', $content, $header));



                     echo("<br><p>Wiadomość została wysłana.</p>");

    }else{
          echo("<p>Spróbuj ponownie</p>");
         }                    



?>

为什么不起作用?当我单击“提交”时,页面会刷新,但没有任何反应。

最佳答案

  1. 第一

使用button而不是submit以防止刷新。

更改<input type="submit" id="contact_button" value="Wyślij" /><input type="button" id="contact_button" value="Wyślij" />

关于javascript - 通过表单发送电子邮件,无需刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20420106/

相关文章:

javascript - Laravel Vuejs 自定义 Css 和 Js 文件

javascript - 将信用卡读卡器连接到 Web 应用程序?

javascript - 当 document.hidden 为 true 时,Chrome 中不会触发滚动事件。任何解决方法?

javascript - 在输出中使用 AJAX/多个 sql 语句显示外键描述而不是值

jquery ui 对话框初始位置

javascript - 如何在渲染另一个 View 之前清除 View ?主干网.js

javascript - 在没有 div 的情况下将 Javascript 字符串插入到 html 中的最简单方法?

php - 错误 : Undifined Variable when creating new file

php - ajax成功查看哪个ECHO自带

javascript - 缩放后 highchart/highstock 工具提示不调整