简体   繁体   English

提交电子邮件表单而无需刷新页面NO JQUERY

[英]Submit email form without page refresh NO JQUERY

I am trying to make form that submits without a page refresh. 我正在尝试制作无需刷新页面即可提交的表单。 I cannot use jQuery to accomplish this. 我无法使用jQuery完成此操作。 I can't seem to find any solution anywhere that DOESN'T mention it, so I just want to know what I am doing wrong with what I have so far, and how I can fix it. 我似乎找不到任何未提及的解决方案,所以我只想知道到目前为止我做错了什么以及如何解决。

html HTML

<form id="contactForm" name="contactForm" method="post" onsubmit="return validate(this);">
<fieldset>
<ol>
<li><label for="email">Email Address</label><input class="text" id="email" name="email" size="30" type="text"></li>
<li><label for="firstName">First Name</label><input class="text" id="firstName" name="firstName" size="30" type="text"></li>
<li><label for="lastName">Last Name</label><input class="text" id="lastName" name="lastName" size="30" type="text"></li>
<li><label for="message">Message</label><textarea cols="40"  id="message" name="message" rows="20"></textarea></li>
</ol>
<input class="button" id="contactSubmit" name="submit" type="submit" value="submit">
</fieldset>
</form>

Ajax/JS 阿贾克斯/ JS

function observeEvent(target, eventName, observerFunction, useCapture){
    if (target.addEventListener) {
        target.addEventListener(eventName, observerFunction, useCapture);
    } else if (target.attachEvent) {
        target.attachEvent("on" + eventName, observerFunction);
    }
}


function start() {
    var xmlhttp = new XMLHttpRequest();
    var contentDiv = document.getElementById("success");

    xmlhttp.open("POST", "processor.php", true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            contentDiv.innerHTML=xmlhttp.responseText;
            alert('message sent!');
        }
    }
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlhttp.send("email=" + email +"&firstName=" + firstName + "&lastName=" + lastName + "&message=" + message);
}

observeEvent(window,"load",function() {
    var btn=document.getElementById("contactSubmit");
    observeEvent(btn,"click",start);
});

PHP for good measure PHP很好

<?php

$fName = $_POST['firstName'];
$lName = $_POST['lastName'];
$email = $_POST['email'];
$message = $_POST['message'];

$to = 'xxx@xxx.com';
$subject = 'the subject';
$message = 'FROM: ' . $fName . ' ' . $lName . ' Email: ' . $email . 'Message: '. $message;
$headers = 'From: ' . $to . "\r\n";

$mailRegex = '/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/';

if (preg_match($mailRegex, $email)) { // this line checks that we have a valid email address
mail($to, $subject, $message, $headers);
echo "Your email was sent!"; 
}

Got it!!!! 得到它了!!!!

I wasn't giving any values to be sent from the form. 我没有提供任何要从表单发送的值。 I fixed it by making my start() function look like this: 我通过使start()函数看起来像这样来修复它:

function start() {
    var xmlhttp = new XMLHttpRequest();
    var contentDiv = document.getElementById("success");

    var email = document.getElementById('email').value;
    var fName = document.getElementById('firstName').value;
    var lName = document.getElementById('lastName').value;
    var mes = document.getElementById('message').value;

    xmlhttp.open("POST", "processor.php", true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4 && xmlhttp.status==200) {
            contentDiv.innerHTML=xmlhttp.responseText;
            alert('message sent!');
        }
    }
    xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
    xmlhttp.send('email=' + encodeURIComponent(email) + '&firstName=' + encodeURIComponent(fName) + '&lastName=' + encodeURIComponent(lName) + '&message=' + encodeURIComponent(mes));

    return false;
}

I had tried to use the values from a validating function beforehand, but there was a scope issue related to that. 我曾尝试过使用验证函数中的值,但是存在与此相关的范围问题。 This works!!! 这有效!!! I'm a real programmer (sort of)! 我是一个真正的程序员(有点)!

The easiest way to do this is just to have an iframe in your document and hide it with CSS. 最简单的方法是在文档中添加一个iframe并使用CSS隐藏它。 Give the iframe a name and then set a target attribute on your form which refers to your hidden iframe. 给iframe命名,然后在表单上设置一个目标属性,该属性引用隐藏的iframe。

This will cause the form to post inside the hidden iframe and the whole page will not reload. 这将导致表单在隐藏的iframe中发布,并且整个页面都不会重新加载。

No javascript is even required. 甚至不需要javascript。

If you need to process the result of the post, just use the onload event on the iframe to detect and process the output of the php page, which is now in the iframe. 如果您需要处理发布的结果,只需在iframe上使用onload事件即可检测并处理php页面的输出,该页面现在位于iframe中。

There two ways to fix it: 有两种解决方法:

  • Expression in form onsubmit attribute should return false ; 形式为onsubmit属性的表达式应返回false ;

  • You should call event.preventDefault method in handler of form submit event or input[type="submit"] click event. 您应该在表单submit事件或输入[type =“ submit”] click事件的处理程序中调用event.preventDefault方法。

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

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