Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/443.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript POST表单异步调用php文件_Javascript_Php_Ajax_Forms_Email - Fatal编程技术网

Javascript POST表单异步调用php文件

Javascript POST表单异步调用php文件,javascript,php,ajax,forms,email,Javascript,Php,Ajax,Forms,Email,有人能帮我吗? 我正在尝试制作一个AJAX联系人表单,下面是我想要做的: 通过JS验证HTML POST表单上的特定格式。 如果有效,启用表单提交按钮;在被按下之后。 向处理post请求的mail.php文件发出XML请求。 根据mail.php响应修改DOM。 这是我的密码: <?php // bool mail( string $to, string $subject, string $message [, string $additional_headers [, string $a

有人能帮我吗? 我正在尝试制作一个AJAX联系人表单,下面是我想要做的:

通过JS验证HTML POST表单上的特定格式。 如果有效,启用表单提交按钮;在被按下之后。 向处理post请求的mail.php文件发出XML请求。 根据mail.php响应修改DOM。 这是我的密码:

<?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";
?>
剧本成功了。只有表单将我重定向到/mail.php,并且只显示0。这意味着邮件本应已发送,但电子邮件甚至不在我的垃圾邮件邮箱中。我猜我必须在我的工作站上配置一个邮件服务器来测试这类东西,而我还没有一个主机用于测试

关于浏览器加载重定向到“/mail.php”,您认为我需要使用GET吗因为我真的在期待一个结果。由于我将使用GET,我的php将需要使用$\u请求而不是$\u POST,对吗


很抱歉,这可能太过分了,但php不是我的强项。

页面被重定向,因为您将表单视为普通html表单提交,并且您需要将表单提交到/mail.php。将方法从_POST更改为_GET不会改变这一点。如果你不希望页面被重定向,我可以考虑几个选项。。。1将表单提交到页面内的iframe,即2使用jQuery收集表单详细信息,并在正常表单行为之外提交$.post请求。使用GET而不是post不会产生任何差异。由于您将使用GET,因此应该使用$\u GET而不是$\u POST。这里:这是一个很好的答案,解释了post、get和request的用法。好的,我想我将尝试使用jQuery并分享我的结果。但是关于不在gmail帐户上接收邮件,我是否需要其他东西让php邮件功能工作?关于邮件,我建议使用PEAR。还要和你的主人核实一下。它们可能不允许通过本地主机发送邮件。
<form method=post action="./mail.php">
    <fieldset><label for=email>Correo el&eacute;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&iacute;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>
function contactSubmitStatus() {
    var form = document.forms[0];
    if(request.readyState === 4)
        if(request.status === 200)
            if(request.responseText == "0") {
                document.getElementsByTagName("h3")[0].innerHTML = "&iexcl;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&aacute;n aparentemente bien pero hubo un problema en el servidor. Por favor intenta mas tarde o envianos un correo electr&oacute;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);
        }
    });
}