簡體   English   中英

通過表單發送電子郵件而不刷新

[英]Sending email by form without refreshing

我正在創建具有視差滾動的頁面,聯系方式框在最后一節。 如果我使用簡單的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" />

嘗試序列化表單數據以避免不必要的geting輸入值並阻止表單(提交)的默認操作。

$("form").on("submit", function(event) {
    event.preventDefault();
    var formData = $(this).serialize();
    $.ajax({
        url: 'inc/contact.php',
        data: formData,
        type: 'POST',
        success: function(odp){
            alert(odp);
        }
    });
});

嘗試刪除標簽周圍的包裝器(你已經有一個jquery ajax()調用POST)並添加$ _POST ['name']作為其他提到的實際初始化你的php變量。

您需要阻止表單在您的jquery中提交。 嘗試這個:

$('#formularz').submit(function ( e ) {


    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);
            }

           });

    e.preventDefault();
});

刪除表單標簽...它強制您的頁面刷新。它將適合您。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM