0
我在YouTube上关于设置联系页面的一个教程,出于某种原因我得到一个错误信息在控制台说submitForm没有定义,当我按下提交。我的问题是我有相同的代码在另一个网站上工作,但是当我复制完全相同的代码时,它不起作用。这里是我的js代码:Php联系表格参考错误
function _(id){ return document.getElementById(id); }
function submitForm(){
_("mybtn").disabled = true;
_("status").innerHTML = 'please wait ...';
var formdata = new FormData();
formdata.append("n", _("n").value);
formdata.append("e", _("e").value);
formdata.append("m", _("m").value);
var ajax = new XMLHttpRequest();
ajax.open("POST", "example_parser.php");
ajax.onreadystatechange = function() {
if(ajax.readyState == 4 && ajax.status == 200) {
if(ajax.responseText == "success"){
_("my_form").innerHTML = '<h2>Thanks '+_("n").value+', your message has been sent.</h2>';
} else {
_("status").innerHTML = ajax.responseText;
_("mybtn").disabled = false;
}
}
}
ajax.send(formdata);
}
这里是我的PHP代码:
<?php
if(isset($_POST['n']) && isset($_POST['e']) && isset($_POST['m'])){
$n = $_POST['n']; // HINT: use preg_replace() to filter the data
$e = $_POST['e'];
$m = nl2br($_POST['m']);
$to = "[email protected]";
$from = $e;
$subject = 'Contact Form Message';
$message = '<b>Name:</b> '.$n.' <br><b>Email:</b> '.$e.' <p>'.$m.'</p>';
$headers = "From: $from\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\n";
if(mail($to, $subject, $message, $headers)){
echo "success";
} else {
echo "The server failed to send the message. Please try again later.";
}
}
?>
正如你可以看到我定义的功能,但收到这个错误。任何帮助将不胜感激。
你怎么把你的HTML中的JavaScript?直接在'