如何通过 ajax 调用显示 PHP 文件的回显 运行?

How do I display the echo of a PHP file that is ran by an ajax call?

我正在做的一个项目要求我在 Joomla 中构建一个邮件表单。

SO 聊天中的一些人建议我使用 PHPmailer - 但我注意到它包含在 joomla 包中,我真的不想第二次加载它,所以我阅读了一些关于 jFactory 的文档,然后我去了那个方式。

我想用一些 javascript 来个性化输入字段,所以我还使用了 JS(具体来说,ajax 调用,您可以在下面阅读)命令服务器向我发送邮件。我不知道是否 possible/easier 将邮件字段传递给邮件程序部分,如果它驻留在表单所在的同一 php 文件中,所以我创建了一个 html 表单使用实际发件人调用 JS 和单独的 php:

一切正常,但我需要更多测试,我无法从任何地方访问 php 回显,包括 chrome检查员.

我读到 echo 会自动保存到 javascript 中的一个 "data" 变量,但是这个变量不存在,如果我创建它(可能在错误的地方),它不会'得到填充。

我已经尝试(在 php 脚本中)try/catch 和 if($mail->Send()) 方法来生成回显消息,但是,因为我从来没有 运行 浏览器中的 php 页面,我不知道两者是否正常。

如何将回显消息返回给我的 JS,以便我可以使用 console.log() 来阅读它? 我无法在 Chrome 上安装扩展程序,也无法在不久的将来使用任何其他浏览器。但如果那是我唯一的选择,我想知道。

我将省略 headers、_JEXEC 函数和大部分文件,以便仅显示 php 调用部分。

js:

function send(datastr) {
  $.ajax({
    type: "POST",
    url: "formsend.php",
    data: datastr,
    cache: false,
    success: function(html) {
      $("#systemMessage").fadeIn("slow");
      $("#systemMessage").html('<span>Message successfully sent.</span>');
      $("#systemMessage").css("background-color", "#e1ffc0");
      setTimeout($("#systemMessage").fadeOut("slow"), 2000);
      console.log(data);
    }
  });
}

php:

try {
  define('JPATH_BASE', "../");
  define( 'DS', DIRECTORY_SEPARATOR );

  require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

  require ('libraries/joomla/factory.php');
  # require_once '../class.phpmailer.php';

  $name=$_REQUEST['name']; 
  $subject=$_REQUEST['subject'];
  $body=$_REQUEST['body']; 
  $from=$_REQUEST['from'];

  $to = "mymail@me.com";;

  # Invoke JMail Class
  $mail = JFactory::getMailer();
  # $mail->isSMTP; is not working

  # Set sender array so that my name will show up neatly in your inbox
  $sender = array($user, $name);
  $mail->setSender($sender);

  # Add a recipient
  $mail->addRecipient($to);
  $mail->ClearCCs();
  $mail->ClearBCCs();

  $mail->setSubject($subject);
  $mail->setBody($body);

  $mail->ClearAttachments();
  $mail->ClearCustomHeaders();

  # Send once you have set all of your options
  $mail->Send();
  echo "Message Sent OK\n";
}
catch (Exception $e) {
echo $e->getMessage();
}

检查 chrome 开发工具上的网络选项卡,在那里你可以看到服务器的响应,这应该很简单

function send(datastr) {
  $.ajax({
    type: "POST",
    url: "formsend.php",
    data: datastr,
    cache: false,
    success: function(html) {
      console.log(html);
    }
  });
}

你的变量必须相同。如果您使用 success: function (html) 那么您的 console.log 应该是 console.log(html) 否则如果您决定使用 data 那么您将 html 更改为 data

js:

function send(datastr) {
  $.ajax({
    type: "POST",
    url: "formsend.php",
    datatype : 'json',
    data: datastr,
    cache: false,
    success: function(html) {
      $("#systemMessage").fadeIn("slow");
      $("#systemMessage").html('<span>Message successfully sent.</span>');
      $("#systemMessage").css("background-color", "#e1ffc0");
      setTimeout($("#systemMessage").fadeOut("slow"), 2000);
      console.log(html);
    }
  });
}

php:

try {
  define('JPATH_BASE', "../");
  define( 'DS', DIRECTORY_SEPARATOR );

  require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
  require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

  require ('libraries/joomla/factory.php');
  # require_once '../class.phpmailer.php';

  $name=$_REQUEST['name']; 
  $subject=$_REQUEST['subject'];
  $body=$_REQUEST['body']; 
  $from=$_REQUEST['from'];

  $to = "mymail@me.com";;

  # Invoke JMail Class
  $mail = JFactory::getMailer();
  # $mail->isSMTP; is not working

  # Set sender array so that my name will show up neatly in your inbox
  $sender = array($user, $name);
  $mail->setSender($sender);

  # Add a recipient
  $mail->addRecipient($to);
  $mail->ClearCCs();
  $mail->ClearBCCs();

  $mail->setSubject($subject);
  $mail->setBody($body);

  $mail->ClearAttachments();
  $mail->ClearCustomHeaders();

  # Send once you have set all of your options
  $mail->Send();
  echo json_encode(array('status' => 'success','message'=>"Message Sent OK"));
}
catch (Exception $e) {
  echo json_encode(array('status' => 'failed','message'=>$e->getMessage()));
}