簡體   English   中英

jQuery / ajax表單-發送到php問題

[英]jQuery/ajax form - send to php problem

我的網站上有一個外觀很好的slideup / slidedown jQuery表單。 它將數據發送到同一文件以發送電子郵件。 這工作正常:

$(document).ready(function(){
    $('div.contact a.submit').click(function() {
        var name = $('div.contact input.name').val();
        var email = $('div.contact input.email').val();
        var message = $('div.contact textarea.message').val();
        $('div.contact').slideUp('slow', function() {
            $.ajax({
                type: "POST",
                url: "test.php",
                data: "name=" + name + "&email=" + email + "&message=" + message,
                success: function(msg)
                {
                    $('div.contact').html('<h1>Contact</h1><div class="comment">Success!</div><div class="clear"></div><p class="indent">Thank you, we will be in contact shortly.</p>');
                    $('div.contact').slideDown('slow');
                }//end of success
            });//end of ajax
        });
    });
});

在test.php頂部的php發送電子郵件:

include("expander-b1.0.php");
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
sendmail("admin@site.co.uk", $message, "Contact message from $name", $email);

這是從外部文件獲取我的簡單郵件功能。 但是,我想采用某種方法來驗證表單條目(包括電子郵件驗證),然后在聯系人列表中顯示錯誤。 我對jQuery或Ajax經驗不足,但是無法在php中使用if語句並在ajax的“成功”部分中回顯變量,因此無法使用它。

$(document).ready(function(){
    $('div.contact a.submit').click(function() {
        var name = $('div.contact input.name').val();
        var email = $('div.contact input.email').val();
        var message = $('div.contact textarea.message').val();

        //Email Validation
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
        if(reg.test(email) == false) {
            alert('Invalid Email Address');
            return false;
        }
        //Name and message VALIDATION
        //-------- goes here ------------//

        $('div.contact').slideUp('slow', function() {
            $.ajax({
                type: "POST",
                url: "test.php",
                data: "name=" + name + "&email=" + email + "&message=" + message,
                success: function(msg)
                {
                    $('div.contact').html('<h1>Contact</h1><div class="comment">Success!</div><div class="clear"></div><p class="indent">Thank you, we will be in contact shortly.</p>');
                    $('div.contact').slideDown('slow');
                }//end of success
            });//end of ajax
        });
    });
});

您需要使用mysql_real_escape()過濾帖子中的惡意代碼,並且可以使用正則表達式檢查有效的電子郵件。 如果您使用google進行搜索,則會發現很多與此有關的文檔和教程。

您還可以使自己變得容易,並購買(或找到一個免費的)現成的驗證類-> Codecanyon驗證類

關於成功部分,請看這個問題-> 如何創建成功后退功能?

http://www.position-absolute.com/articles/jquery-form-validator-because-form-validation-is-a-mess/

基於JQuery的驗證腳本,在代碼的一部分驗證失敗時,可以很好地驗證並自動插入錯誤消息。 只需包含文件,添加jquery(有關最佳方法,請參見示例源),然后將“ required”元素添加到類名中。 應該是您問題的完美解決方案。...無需瘋狂的數學運算或單獨的選擇器。

暫無
暫無

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

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