有人可以帮我吗? 我正在尝试制作一个AJAX联系表单,这就是我想要做的事情:
这是我的代码:
<?php
// bool mail( string $to, string $subject, string $message [, string $additional_headers [, string $additional_parameters ] ] )
if(isset($_POST['email'])) {
$email_to = "my_test_mail@gmail.com";
$email_subject = "Correo de pruebas";
$email = $_POST['email'];
$name = isset($_POST['nombre']) ? $_POST['nombre'] : "Sin nombre";
$telephone = isset($_POST['telefono']) ? $_POST['telefono'] : "Sin numero";
$nopal = isset($_POST['tor-nopal']) ? "Si" : "No";
$maiz = isset($_POST['tor-maiz']) ? "Si" : "No";
$harina = isset($_POST['tor-harina']) ? "Si" : "No";
$message = $_POST['mensaje'];
function clean_string($string) {
$bad = array("content-type", "bcc:", "to:", "cc:", "href");
return str_replace($bad, "", str_replace("\n", "\r\n", $string));
}
$email_message = "Nombre: ".clean_string($name)."\r\n";
$email_message .= "Correo electronico: ".clean_string($email)."\r\n";
$email_message .= "Telefono: ".clean_string($telephone)."\r\n";
$email_message .= "Nopal: ".clean_string($nopal)."\r\n";
$email_message .= "Maiz: ".clean_string($maiz)."\r\n";
$email_message .= "Harina: ".clean_string($harina)."\r\n\r\n";
$email_message .= "Mensaje:\n".clean_string($message)."\r\n";
$headers = "MIME-Version: 1.0\r\n".
"Content-Type: text/plain;charset=UTF-8\r\n".
"From: Pruebas <juanc_94_konoha@hotmail.com>\r\n".
"Reply-To: ".$email."\r\n".
"Subject: Contacto tortillas\r\n".
"X-Mailer: PHP/".phpversion();
if( mail($email_to, $email_subject, $email_message, $headers) )
echo "0";
else
echo "1";
} else
echo "-1";
?>
我的HTML表单:
<form method=post action="./mail.php">
<fieldset><label for=email>Correo eléctronico:</label>
<input type=email name=email id=email placeholder="usuario@ejemplo.com.mx" required></fieldset>
<fieldset><label for=nombre>Nombre:</label>
<input type=text name="nombre" id=nombre></fieldset>
<fieldset><label for=telefono>Telefono:</label>
<input type=tel name="telefono" id=telefono></fieldset>
<fieldset><label><input type=checkbox name="tor-nopal"> Nopal</label>
<label><input type=checkbox name="tor-maiz"> Maíz</label>
<label><input type=checkbox name="tor-harina"> Harina</label></fieldset>
<fieldset><label for="mensaje">Mensaje</label>
<textarea max-width=500 name="mensaje" id=mensaje max-length=498 required></textarea></fieldset>
<br /><br />
<input type=submit value=Enviar>
</form>
这是我的JS代码的基本草案:
function contactSubmitStatus() {
var form = document.forms[0];
if(request.readyState === 4)
if(request.status === 200)
if(request.responseText == "0") {
document.getElementsByTagName("h3")[0].innerHTML = "¡Gracias por contactarnos!";
fade("out", 1500, form, IE_check());
form.remove;
} else if(request.responseText == "1") {
var explanation = document.createElement("p");
explanation.innerHTML = "Tus datos están aparentemente bien pero hubo un problema en el servidor. Por favor intenta mas tarde o envianos un correo electrónico desde tu cliente de correos. (i.e: https://mail.google.com/)";
explanation.setAttribute("id", "warning-node");
form.appendChild(explanation);
fade("in", 1000, explanation, IE_check());
}
}
function contactForm() {
document.getElementsByName("email")[0].addEventListener("blur", isEmail);
document.getElementsByName("mensaje")[0].addEventListener( "blur", isMsgValid);
document.getElementsByName("submit")[0].addEventListener("click", function() {
var request = createRequest(),
form = document.forms[0],
p = document.createElement("p");
if(request == null) {
alert("Tu navegador no permite transacciones asincronas, prueba con otro navegador.");
return;
} else {
request.onreadystatechange = contactSubmitStatus;
request.open("POST", form.action, true);
request.send(new FormData(form));
p.setAttribute("id", "contact-status");
p.innerHTML = "Procesando datos...";
form.appendChild(p);
}
});
}
脚本有效。只有表单将我重定向到/mail.php并且只显示0.这意味着邮件应该已经发送但电子邮件甚至不在我的垃圾邮箱中。我猜我必须在我的工作站上配置一个邮件服务器来测试这类东西,而且我还没有托管用于测试。
关于浏览器加载重定向到'/mail.php',您认为我需要使用GET吗? '因为我实际上期待一个结果可以采取行动。因为我会使用GET我的php需要使用$ _REQUEST而不是$ _POST,对吗?
对不起,也许这是压倒性的,但PHP不是我的强项。