简体   繁体   English

jQuery / ajax表单-发送到php问题

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

I have a nice looking slideup/slidedown jquery form on my website. 我的网站上有一个外观很好的slideup / slidedown jQuery表单。 It sends the data to the same file to send the email. 它将数据发送到同一文件以发送电子邮件。 This works fine: 这工作正常:

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

The php at the top of test.php to send the email: 在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);

This is getting my simple mail function from a external file. 这是从外部文件获取我的简单邮件功能。 However, I would like some way to validate the form entry (including email validation), then display the error(s) in the contact dive. 但是,我想采用某种方法来验证表单条目(包括电子邮件验证),然后在联系人列表中显示错误。 I am not that experienced with jQuery or ajax but an unable to get it working with using if statements in my php and echoing the variables in the "success" part of the ajax. 我对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
        });
    });
});

you need to use mysql_real_escape() to filter evil code in the post and you can use regular expression to check for a valid email. 您需要使用mysql_real_escape()过滤帖子中的恶意代码,并且可以使用正则表达式检查有效的电子邮件。 if you google for it you will find a lot of documentation and tutorials about that. 如果您使用google进行搜索,则会发现很多与此有关的文档和教程。

you can also make it easy on yourself and buy (or find a free one) a ready to use validation class -> Codecanyon validation class 您还可以使自己变得容易,并购买(或找到一个免费的)现成的验证类-> Codecanyon验证类

and about the success part have a look at this question -> how can i create a success back function? 关于成功部分,请看这个问题-> 如何创建成功后退功能?

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

A JQuery-based validation script that works very well to validate and automatically insert error messages if a section of your code fails validation. 基于JQuery的验证脚本,在代码的一部分验证失败时,可以很好地验证并自动插入错误消息。 Simply include files, add jquery (see source of examples for best methods) and add the "required" element to your class names. 只需包含文件,添加jquery(有关最佳方法,请参见示例源),然后将“ required”元素添加到类名中。 Should be a perfect solution to your problem....with no crazy math or individual selectors required. 应该是您问题的完美解决方案。...无需疯狂的数学运算或单独的选择器。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM