简体   繁体   English

通过html页面通过PHP和jquery / ajax发送电子邮件

[英]Sending an email via PHP and jquery/ajax from a html page

I have a very simple question but its been bugging me for quite some time .I have a html contact us page in which I have a simple form which already has validation assigned to it.The form code is : 我有一个非常简单的问题,但是它困扰了我一段时间。我有一个html与我们联系页面,其中有一个简单的表单,该表单已经分配了验证。表单代码为:

    <div class="contact_form">      
  <form method="post" id="contactForm" name="contactForm" action="">
                    <fieldset class="contactFieldset">
                        <ul>
                            <li>
                                <label for="contactName" class="leftLabel">*Name:</label>
                                <input type="text" name="contactName" id="contactName" class="contactInput required" value="" />

                            </li>
                            <p></p>
                            <li>
                                <label for="email" class="leftLabel">*Email:</label>
                                <input type="text" id="email" name="email" class="contactInput email required" value="" />
                            </li>
                          <span class="simple-success">I'll be in  touch soon</span>
                            <li>
                                <label for="subject" class="leftLabel">*Subject:</label>
                                <input type="text" name="subject" id="subject" class="contactInput required" value="" />
                            </li>
                            <p></p>
                            <li>
                                <label for="message" class="leftLabel">*Message:</label>
                                <textarea rows="10" cols="40" id="message" name="message" class="contactTextarea required"></textarea>

                            </li>
                           <p></p>
                            <li>


                                <input type="submit" alt="Submit button" name="submit" class="submit" id="submit">

                            </li>
                        </ul>
                    </fieldset>
                </form>


</div>       

The code which I am using to try and call the php form using ajax is this 我用来尝试使用ajax调用php表单的代码是这个

$(document).ready(function() {

    //if submit button is clicked
    $('#submit').click(function () { 

 alert("test i am here");



        /*get the email value*/

        var email = $("input#email").val();
        var name = $("input#contactName").val();
        var subject = $("input#subject").val();
        var message=$("input#message").val();
        alert("email"+email);

/* Check if the email is good or bad */

var goodEmail = email.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\.sex)|(\.biz)|(\.aero)|(\.coop)|(\.museum)|(\.name)|(\.pro)|(\.arpa)|(\.asia)|(\.cat)|(\.int)|(\.jobs)|(\.tel)|(\.travel)|(\.xxx)|(\..{2,2}))$)\b/gi);
    apos=email.indexOf("@");dotpos = email.lastIndexOf(".");lastpos=email.length-1;
    var badEmail    = (apos<1 || dotpos-apos<2 || lastpos-dotpos<2);

/*If the email is bad ,display the error message*/

if (email=="" || !goodEmail || badEmail) {

        $("email").focus();
      return false;
    }


        var dataString = 'email='+ email + '\n Name='+ name+ '\n Subject='+ subject+ '\n message='+ message;
        alert (dataString);

        $.ajax({
      type: "POST",
      url: "mai.php",

      data: dataString,
      //Do not cache the page
            cache: false,
      success: function(html) {
        $('.simple-sucess').fadeIn(100).show();
        $('.contact_form').fadeOut(100).hide();
        $('.simple_error').fadeOut(100).hide();


      }
     });
    return false;
    });
});

The thing is the alert is not even being displayed when I press the submit button..what am I doing wrong here? 问题是当我按下“提交”按钮时甚至没有显示警报。我在这里做错了什么?

The validation code is 验证码是

<script type="text/javascript">
    jQuery(document).ready(function($){
        $("#contactForm").validate();
    });

First of all, use the submit event, not the submit button click event because the submit button is already wired up to do a normal submit. 首先,使用提交事件,而不是提交按钮单击事件,因为提交按钮已经连接好以进行常规提交。 There may also be a bug, be sure to check your javascript console for errors. 可能还有一个错误,请务必检查您的JavaScript控制台是否有错误。 Either way... 无论哪种方式...

What you probably really want to do is use the jQuery form plugin which will make your code a lot more simple. 您可能真正想做的是使用jQuery form插件 ,它将使您的代码更加简单。

Then your revised code would be as simple as: 然后,您修改后的代码将非常简单:

$('#contactForm').ajaxForm(function() { 
            $('.simple-sucess').fadeIn(100).show();
            $('.contact_form').fadeOut(100).hide();
            $('.simple_error').fadeOut(100).hide()
        });

In this case you would lose your email validation, but why reinvent the wheel, there are tons of validators out there that already have the bugs worked out etc. 在这种情况下,你会失去你的电子邮件验证,但为什么推倒重来,有万吨验证的,在那里,已经上了漏洞等。

the first thing is you are using : 首先是您正在使用:

<input type="submit" alt="Submit button" name="submit" class="submit" id="submit">

in your form, and in jquery you are using .click() event, 在您的表单中,并且在jquery中,您正在使用.click()事件,

if try to change 如果尝试改变

<input type="submit" alt="Submit button" name="submit" class="submit" id="submit">

to : 至 :

<input type="button" alt="Submit button" name="submit" class="submit" id="submit">

then it will work perfectly with the .click() event 那么它将与.click()事件完美配合
or the second option you have if you don't want to change the input type then use .submit() instead of .click() 或第二个选项,如果您不想更改输入类型,请使用.submit()而不是.submit() .click()

OMG, so many code lines. 天哪,这么多代码行。 A little suggestion: keep it simple enough to debug. 一个小建议:保持足够简单以进行调试。 A jsfiddle demo is recommended for better answers. 建议使用jsfiddle演示以获得更好的答案。

Here I post my solution for ajax forms, which works in basic browsers without javascript support. 在这里,我发布了针对Ajax表单的解决方案,该解决方案可以在没有JavaScript支持的基本浏览器中使用。

html: HTML:

<form method="post" id="contactForm" action="somewhere">
    Name: <input type="text" name="contactName" />
    <br />
    <input type="submit" value="Submit this form" />
</form>

javascript: javascript:

jQuery(function($){
    $('#contactForm').submit(function(e){
        e.preventDefault?e.preventDefault():false;
        $.post(this.action,$(this).serialize(),function(text){
            //callbacks
            console.log(text);
        });
        return false;
    })
});

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

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