ajax联系表单不会将POST数据发送到php

时间:2015-03-08 20:08:04

标签: php html ajax

我制作了一个html格式的宽度ajax:

    function confirmSendMail() {

    $(".send_contact").click(function(event){

        event.preventDefault(); 

        var nome = $("#inputName").val();
        var email = $("#inputEmail").val();
        var telefono = $("#inputTelefono").val();
        var messaggio = $("#message").val();

        $.ajax({

            type: "POST",

            url: "send.php",

            data: "nome=" + nome + "&email=" + email + "&telefono=" + telefono + "&messaggio=" + messaggio,

            success: function(msg) {
                alert('messaggio inviato!')
            },

            error: function() {
                alert("Si e' verificato un errore imprevisto...");
            }

        }); 

    return false;     

    });

}

警报确认我已发送邮件,并且php引擎发送电子邮件...但它是空的,因为POST不会向php发送数据:

    <?php
require 'PHPMailer-master/class.phpmailer.php';

$mail = new PHPMailer;

$nome = $_POST['inputName'];
$email = $_POST['inputEmail'];
$telefono = $_POST['inputTelefono'];
$messaggio = $_POST['message'];

$mail->From = $email;
$mail->FromName = $email;
$mail->AddAddress('mimelaine.dhampiro@gmail.com');              // Add a recipient

$mail->WordWrap = 50;                                 // Set word wrap to 50 characters
/* $mail->AddAttachment('/var/tmp/file.tar.gz');         // Add attachments
$mail->AddAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name */
$mail->IsHTML(true);                                  // Set email format to HTML

$mail->Subject = 'email dal form acfmavvocati.it';
$mail->Body    = 'nome: ' . $nome . '<br /><br />email: ' . $email . '<br /><br />telefono: ' . $telefono . '<br /><br />messaggio: ' . $messaggio;
/* $mail->AltBody = 'This is the body in plain text for non-HTML mail clients'; */

if($mail->Send()) {
    echo "works";
    exit;
} else {

    echo "doesn't work";
    exit;

}

?>

我尝试了很多解决方案,这让我很生气。

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

问题出在您的PHP文件中。

应该是

$nome = $_POST['nome'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$messaggio = $_POST['messaggio'];

因为您已经在AJAX数据中声明了名称

data: "nome=" + nome + "&email=" + email + "&telefono=" + telefono + "&messaggio=" + messaggio,

答案 1 :(得分:0)

您检查了错误的$ _POST键,这些键未在您的ajax数据字符串中设置:

$nome = $_POST['inputName'];
$email = $_POST['inputEmail'];
$telefono = $_POST['inputTelefono'];
$messaggio = $_POST['message'];

将其更改为:

$nome = $_POST['nome'];
$email = $_POST['email'];
$telefono = $_POST['telefono'];
$messaggio = $_POST['messagio'];