AJAX提交表单以奇怪的方式发送

时间:2017-01-06 21:43:00

标签: php ajax forms

首先,我是初学者。所以,我在Bootstrap 3.3.7中有一个表单。单击“发送消息”按钮后,应直接从站点发送到电子邮件。

虽然当我点击“发送消息”时,它会打开outlook(不是直接从网站发送),消息是:

name=Test&email=test%40hotmail.com&message=Test&submit=Send+Message

有谁知道为什么会这样? 这是代码:

HTML:

    <form action="mailto:email@exemple.com?Subject=Exemple%20Contact" onsubmit="return submitForm();" method="post" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">
              <div class="col-md-6">
                <div class="row">
                  <div class="col-md-12">

                   <input placeholder="Name" class="input-field" name="name" required="" type="text">

                 </div>



                 <div class="col-md-12">
                   <input placeholder=" Email" class="input-field" name="email" required="" type="email"> 
                 </div>
               </div>
             </div>
             <div class="col-md-6">
               <textarea placeholder=" Message" class="input-field" name="message"></textarea> 
               <input value="Send Message" class="input-send submit" type="submit" name="submit">
             </div>




             <div class="col-md-12 send no-show hidden-xs"><button class=" botao btn btn-sucess"><h4 class="">Mensagem enviada com sucesso! Entraremos em contato brevemente!</h4></button></div>
      </form>


JS:

    function submitForm() {
        $.ajax({type:'POST', url:'email-action.php', data:$('#contact-form').serialize(),
         success: function(response) {
         $('.submit').html('send');
         $('.send').removeClass('no-show');
         document.contactform.reset();                                 
       }});                
        return false;

  }



PHP:

    $myemail = 'email@exemple.com';

    if (isset($_POST['name'])) {
    $name = strip_tags($_POST['name']);
    $email = strip_tags($_POST['email']);
    $message = strip_tags($_POST['message']);
    echo "Name:".$name."<br>";   
    echo "Email: ".$email."<br>"; 
    echo "Message: ".$message."<br>";

    $to = $myemail;
    $email_subject = "Contact from site";
    $email_body = "You have received a new message. ".
    " Here are the details:\n Name: $name \n ".
    "Email: $email\n Message \n $message";
    $headers = "From: $myemail\n";
    $headers .= "Reply-To: $email";
    mail($to,$email_subject,$email_body,$headers);
    }

1 个答案:

答案 0 :(得分:0)

你是否太混淆了,只需改变第一行:

<form action="" onsubmit="return submitForm();" method="POST" name="contactform" class="contact-form wow zoomIn" data-wow-delay="0.6s" id="contact-form">

操作是您发送数据的URL。如果是相同的URL,则可以将其清空。

有必要把这样的方法:method =&#34; POST&#34;默认情况下,该方法是GET,为此,数据会显示在您的网址

现在,在PHP中,您可以在数组$_POST['someKey']中获取POST参数,或者如果您需要URL中的参数(例如&#34; name = Test&amp; email = test%40hotmail.com&amp; message = Test&amp; submit =发送+消息&#34;)使用$_GET['otherKey']

太基础了,你应该学习一些关于PHP W3Schools的教程,这是一个很好的网页,很多课程。

祝你好运,抱歉我的英语不好。