如何从php文件中获取消息并使用ajax发送到另一个php文件?

时间:2016-11-11 10:56:02

标签: php ajax

$.ajax({
                type: "POST",
                url: "insert.php",
                data: dataString,
                cache: false,
                success: function(result){

                    alert(msg);

                }
        });

我编写了一个ajax文件,将一些参数发送到另一个文件insert.php,以便将这些参数作为邮件发送。如果邮件发送成功,它应该将消息发送回主文件以在那里显示它。我已经编写了insert.php的代码,如下所示:

<?php
 $date=$_POST['date'];
 $newDate = date("d-m-Y", strtotime($date ));

 $time=$_POST['time'];
 $name=$_POST['name'];
 $number=$_POST['number'];
 $mail=$_POST['mail'];
 $pname=$_POST['pname'];
 $ptype=$_POST['ptype'];
 $gender=$_POST['gender'];
 $comments=$_POST['comments'];
 $to  = 'df@gmail.com'; 
 $subject = 'Enquiry !!!!';


$message = ' A Customer has made a enquiry about a package  " '.$pname.'  "  whose Name is  "  '.$name.'  "  with Email-id   '.$mail.' and who    is  a  '.$gender.' and Contact Number is   '.$number.' and has send a message " '.$comments.' " on Date  '.$newDate .'  and Time  '.$time;

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 

$retval = mail($to, $subject, $message, $headers);


if($retval == true)
{
  $msg = "YOUR ENQUIRY HAS BEEN SEND!!!!";
  echo $msg;

}else {
$msg = "YOUR ENQUIRY HAS NOT BEEN SEND!!!! PLEASE TRY AGAIN !!!";
echo $msg;
}
 ?>

我想将$ msg值发送回php文件,控件来自那里显示它。如何在成功函数中获取该消息?我试图提醒它,但它无法正常工作。

1 个答案:

答案 0 :(得分:0)

$.ajax({
    type: "POST",
    url: "insert.php",
    data: dataString,
    cache: false,
    success: function(result){
        alert(result);// the resiult is your message.
    }
});

你从insert.php回复什么,结果就是你的消息。请试试这个你会得到结果。结果是函数的参数。 ajax页面的输出将仅在此处捕获并仅处理此结果。