从Ajax jQuery File接收Php中的数据

时间:2014-05-15 08:46:22

标签: javascript php jquery ajax

我知道php的基本内容。但我从未使用ajax。现在,我有一个联系人文件,他们使用ajax将数据传递到php文件。我真的不知道怎么做。

请帮我解决这个问题。

contact.js:

$.ajax({
             type: "POST",
             url : "send.php",    
             data: "name=" + name + "&email=" + email + "&subject=" + "You Got Email" + "&message=" + message,
             success: function(data){    
              if(data == 'success'){
                $("#btnsubmit").remove();
                $("#mail_success").fadeIn(500);
              }else{
                $("#mail_failed").html(data).fadeIn(500);
                $("#send").removeAttr("disabled").attr("value", "send");
              }     
             }  
           });

我不知道它是否正确,但我知道这一点,所以我自己创建了send.php文件。检查一下。

send.php:

<?php

$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];

$mail_sub= $subject;
$mail_from = "$email"; // applicant email id. it must be dynamic.

$mail_body = "<html> <body>";
$mail_body .= '<table style="" cellpadding="3">';
$mail_body .= "
                <tr>
                <td width='120'> <strong> Name </strong> </td>
                <td width='8'> : </td>
                <td width='300'> $name </td>
                </tr>
                <tr>
                <td> <strong> Email </strong> </td>
                <td> : </td>
                <td> $email </td>
                </tr>
                <tr>
                <td> <strong> Message </strong> </td>
                <td> : </td>
                <td> $message </td>
                </tr>
                </table>
                </body> </html>"; 

$header = "From: $name<$email>\r\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$header .= "X-Priority: 1\n";
$header .= "X-MSMail-Priority: High\n";
$header .= "X-Mailer: PHP / ".phpversion()."\r\n";
$header .= "Importance: High\n";

$mail_to= 'in@example.com'; // receiver email address.

?>

如何实现这一目标?我想发送电子邮件,并希望按照js文件返回消息。

1 个答案:

答案 0 :(得分:0)

phppart

<?php

$name = $_POST["name"];
$email = $_POST["email"];
$subject = $_POST["subject"];
$message = $_POST["message"];

$mail_sub= $subject;
$mail_from = "$email"; // applicant email id. it must be dynamic.

$mail_body = "<html> <body>";
$mail_body .= '<table style="" cellpadding="3">';
$mail_body .= "
                <tr>
                <td width='120'> <strong> Name </strong> </td>
                <td width='8'> : </td>
                <td width='300'> $name </td>
                </tr>
                <tr>
                <td> <strong> Email </strong> </td>
                <td> : </td>
                <td> $email </td>
                </tr>
                <tr>
                <td> <strong> Message </strong> </td>
                <td> : </td>
                <td> $message </td>
                </tr>
                </table>
                </body> </html>"; 

$header = "From: $name<$email>\r\n";
$header .= "MIME-Version: 1.0\n";
$header .= "Content-Type: text/html; charset=\"iso-8859-1\"\n";
$header .= "X-Priority: 1\n";
$header .= "X-MSMail-Priority: High\n";
$header .= "X-Mailer: PHP / ".phpversion()."\r\n";
$header .= "Importance: High\n";

$mail_to= 'in@example.com'; // receiver email address.

$isvalid = false;
if(mail($mail_to, $mailsub, $mail_body, $header)){
  $isvalid = true;
}
echo json_encode($isvalid); 

jquery part

$.ajax({
             type: "POST",
             url : "send.php",    
             data: "name=" + name + "&email=" + email + "&subject=" + "You Got Email" + "&message=" + message,
             success: function(data){
              var response = jQuery.parseJSON(data);
              if(response.isvalid == true){
                $("#btnsubmit").remove();
                $("#mail_success").fadeIn(500);
              }else{
                $("#mail_failed").html(data).fadeIn(500);
                $("#send").removeAttr("disabled").attr("value", "send");
              }     
             }  
           });

嗯那应该这样做!