繁体   English   中英

从Ajax jQuery File接收Php中的数据

[英]Receive data in Php from Ajax jQuery File

我知道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文件发送电子邮件并返回消息。

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部分

$.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");
              }     
             }  
           });

Andd umm应该这样做!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM