我正在尝试为我拥有的网站实现表单插件。它应该很简单,但它拒绝表现得像我想要的那样。
这是一个 div 内的简单 php 电子邮件表单,一旦正确提交,就会被同一 div 内的成功或失败消息替换。现在我的 JS 只是在表单顶部添加消息。
我很震惊,因为我以为我解决了一些严重的问题,因为我这样做的经验,但我无法克服这个简单的错误。我想要的只是包含表单的 DIV 内的响应消息。
这是包含的PHP:
if (isset($_REQUEST['sender']) && isset($_REQUEST['message'])) {//if "sender" and "message" is filled out, proceed
//check if the email address is invalid
$mailcheck = spamcheck($_REQUEST['sender']);
if ($mailcheck==FALSE) {
echo "An error has occurred" . $_REQUEST['sender'] . " " . $_REQUEST['message'];
}
else {//send email
$email = $_REQUEST['sender'] ;
$subject = "This is the subject" ;
$message = $_REQUEST['message'] ;
mail("myself@gmail.com", $subject, $message, "From: $email" );
echo "Your message was sent";
}
}
function createForm($formID, $formName) {
echo "<div id=\"" . $formID . "\" class=\"ajaxForm\" >" . PHP_EOL;
echo "<form name =\"" . $formName . "\" method=\"post\" onsubmit=\"ajaxUpdate(\"" . $formID . "\",\"sandbox.php\"," . sender . "," . message . ");>" . PHP_EOL;
echo "<input name=\"sender\" type=\"text\" placeholder=\"email\" required><br>" . PHP_EOL;
echo "<br>" . PHP_EOL;
echo "<textarea rows=\"6\" cols=\"40\" name=\"message\" placeholder=\"message\" required></textarea><br>" . PHP_EOL;
echo "<input type=\"submit\" value=\"Send\" >" . PHP_EOL;
echo "</form>" . PHP_EOL;
echo "</div>" . PHP_EOL;
}
function spamcheck($field) {
//filter_var() sanitizes the e-mail
//address using FILTER_SANITIZE_EMAIL
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
//filter_var() validates the e-mail
//address using FILTER_VALIDATE_EMAIL
if(filter_var($field, FILTER_VALIDATE_EMAIL)) {
return TRUE;
}
else {
return FALSE;
}
}
和 ajaxUpdate JS:
function ajaxUpdate(targetDIV, responsePHP, param1, param2) {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById(targetDIV).innerHTML = xmlhttp.responseText;
}
}
var strContent = responsePHP + "?sender=" + param1 + "&message=" + param2;
xmlhttp.open("POST", strContent, true);
xmlhttp.send();
}
我通过包含 js 和 php 并使用 createForm() 函数生成表单来使用它。
请一些帮助..