简体   繁体   English

AJAX电子邮件表单不会提交

[英]AJAX email form will not submit

I have an email sign-up form on my site that I recently added validation to. 我的网站上有一个电子邮件注册表单,我最近添加了验证。

Now, the form will not send or provide an error message. 现在,表单不会发送或提供错误消息。 When I check the inspector I see the following error: 当我检查检查员时,我看到以下错误:

TypeError: null is not an object (evaluating 'document.getElementById(update[0]).innerHTML = update[1]') TypeError:null不是对象(评估'document.getElementById(update [0])。innerHTML = update [1]')


This is my contact.php file 这是我的contact.php文件

 <?php
$to = "hello@interzonestudio.com";
$subject_prefix = ""; 

if(!isset($_GET['action']))

$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_GET['email']); //The senders subject
$email = trim($_GET['email']); //The senders email address

if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {

mail($to,$subject,$message,"From: ".$email."");

echo 'contactarea|<div id="thanks">Thank you. We promise you won’t regret it.</div>';

else {
  echo("$email is not a valid email address");
}
?>

This is my form in HTML 这是我在HTML中的表单

<div id="contactarea">
    <span style="font-family: 'Old Standard TT', serif;">Newsletter</span>
    <form id="contactform" name="contactform" >
        <input class ="email" type="text" name="email" id="inputbox" value="E-Mail"
onfocus="if(this.value==this.defaultValue)this.value='';" onblur="if(this.value=='')this.value=this.defaultValue;"/>
        <input type="submit" value="Submit" name="send" onclick="sendemail(); return false; " class="signup" >
        </form>
    </div>

and this is my javascript 这是我的javascript

<script language="javascript">
    function createRequestObject() {
        var ro;
        var browser = navigator.appName;
        if (browser == "Microsoft Internet Explorer") {
            ro = new ActiveXObject("Microsoft.XMLHTTP");
        } else {
            ro = new XMLHttpRequest();
        }
        return ro;
    }
    var http = createRequestObject();

    function sendemail() {
        var email = document.contactform.email.value;
        document.contactform.send.disabled = true;
        http.open('get', 'contact.php?email=' + email + '&action=send');
        http.onreadystatechange = handleResponse;
        http.send(null);

        setTimeout(function() {
            jQuery(document).find("#thanks").fadeOut();
        }, 3000);

    }

    function handleResponse() {
        if (http.readyState == 4) {
            var response = http.responseText;
            var update = new Array();
            if (response.indexOf('|' != -1)) {
                update = response.split('|');
                document.getElementById(update[0]).innerHTML = update[1];
            }
        }
    }
</script>

Any insight would be greatly appreciated. 任何见解将不胜感激。

I think this is what you are looking for: 我认为这就是你要找的东西:
document.contactform.send.disabled=false; document.contactform.send.disabled = FALSE;

add another div in html page with id = "msg" 在html页面中添加另一个id为id =“msg”的div

replace 更换

document.getElementById(update[0]).innerHTML = update[1]; document.getElementById(update [0])。innerHTML = update [1];

with

you can add conditions here 你可以在这里添加条件
depending on what you want to display upload[0] or upload[1] 取决于您要显示的内容[0]或上传[1]
document.getElementById('msg').innerHTML = update[0]+update[1]; document.getElementById('msg')。innerHTML = update [0] + update [1];

and in contact.php 并在contact.php

there is '}' missing before else. 其他地方有'}'缺失。

Multiple errors, client and server-side. 多个错误,客户端和服务器端。

Changes to javascript. 对javascript的更改。 Your form data wasn't being sent in the php call. 您的表单数据未在php调用中发送。

I have made changes to your call type get/post and used new FormData(). 我更改了你的调用类型get / post并使用了新的FormData()。 If you want to add more to your call formdata.append("ParamName", Value/Variable); 如果你想为你的调用添加更多内容formdata.append("ParamName", Value/Variable); and use $something=$_POST['ParamName']; 并使用$something=$_POST['ParamName']; to get the post in PHP. 用PHP获取帖子。

var formdata = new FormData();
formdata.append("email", email);
formdata.append("action", "send");
http.open('POST', 'contact.php');
http.onreadystatechange = handleResponse;
http.send(formdata);

Changes to PHP. 对PHP的更改。 You missed the opening/closing of the if statements. 你错过了if语句的开启/关闭。

The way you have your javascript setup, you split the php reply (|) if the email posted wasn't valid you would cause a JS error because you didn't have the divID and bar(|) in your echo. 你的javascript设置的方式,你拆分php回复(|)如果发布的电子邮件无效你会导致JS错误,因为你的回声中没有divID和bar(|)。

$to = "hello@interzonestudio.com";
$subject_prefix = ""; 
if(isset($_POST['action'])){ // ***** Missing ({) 
$subject = "Newsletter Sign Up"; //The senders subject
$message = trim($_POST['email']); //The senders subject
$email = trim($_POST['email']); //The senders email address
if (!filter_var($email, FILTER_VALIDATE_EMAIL)===false) {
mail($to,$subject,$message,"From: ".$email."");
// **** Div ID Missing with Bar (contactarea|)
echo 'contactarea|<div id="thanks">Thank you. We promise you won\'t regret it.</div>';
// **** Else missing (})
}else {
  echo("contactarea|$email is not a valid email address");
}
}// **** Close if issset  (}) 

I hope I have covered all your problems in this answer. 我希望我在这个答案中涵盖了你所有的问题。

If you don't understand anything please leave a comment below, i will update the answer to help you understand anything within this answer. 如果你什么都不懂,请在下面发表评论,我会更新答案,以帮助你理解这个答案中的任何内容。 I would rather you take this source code understand it, not just a copy paste. 我宁愿你把这个源代码理解它,而不仅仅是复制粘贴。 You won't learn from copy/paste. 您不会从复制/粘贴中学习。

Tip: Clean your php string before putting them into mail(). Tip:在将php字符串放入mail()之前清理它们。

I hope this helps. 我希望这有帮助。 Happy coding! 快乐的编码!

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

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