PHP-使用ajax提交表单,并返回XML

PHP-使用ajax提交表单,并返回XML,php,ajax,xml,domdocument,Php,Ajax,Xml,Domdocument,我在使用jQuery/AJAX提交表单,并返回成功消息+XML文件(用PHP生成)时遇到问题 这就是我现在拥有的: invoice.php: <form method="post" id="invoiceform"> <? /* Populate the form with input later on. For now, the XML data is hardcoded in PHP */ ?> <button type="submit">Submit f

我在使用jQuery/AJAX提交表单,并返回成功消息+XML文件(用PHP生成)时遇到问题

这就是我现在拥有的:

invoice.php

<form method="post" id="invoiceform">
<? /* Populate the form with input later on. For now, the XML data is hardcoded in PHP */ ?>
<button type="submit">Submit form</button>
</form>


//Submit the form:
$('#invoiceform').on('submit', function(e) { //use on if jQuery 1.7+
    e.preventDefault();  //prevent form from submitting
    e.stopImmediatePropagation(); //prevent double.

    //Show the loading message.
    $form = $(this);

     // Use Ajax to submit form data
        $.ajax({
            url: '/api/invoice/invoice_converter',
            type: 'POST',
            data: $form.serialize(),
            dataType: "json",
            success: function(data) {

                            console.log(data);

                if (data.result == 'success') {
                //Success
                $(".status").html(data.message);

            } else {
                $(".status").html(data.message);
           }
        },
        error: function(data) {
            console.log("Something went wrong!");
            console.log(data);

        }
    });

return false;

});
$invoice = new Invoice;
if($_POST)
{
    $convertInvoice = $invoice->convertInvoice();

    if($convertInvoice == 1){
              $error = "Error: Error message goes here.";
              $stop = true;
    }
    if($stop){
        $result = array("result" => "error","message" => $error);
    }else{

        $result = array("result" => "success","message" => $convertInvoice);

    }

}
header('Content-type: application/json');
echo json_encode($result);
function convertInvoice(){

    /* create a dom document with encoding utf8 */
      $domtree = new DOMDocument('1.0', 'UTF-8');

      /* create the root element of the xml tree */
      $xmlRoot = $domtree->createElement("xml");
      /* append it to the document created */
      $xmlRoot = $domtree->appendChild($xmlRoot);

      $currentTrack = $domtree->createElement("track");
      $currentTrack = $xmlRoot->appendChild($currentTrack);

      /* you should enclose the following two lines in a cicle */
      $currentTrack->appendChild($domtree->createElement('charge','letter'));
      $currentTrack->appendChild($domtree->createElement('description','Payable cover letters'));

      $currentTrack->appendChild($domtree->createElement('charge','vat'));
      $currentTrack->appendChild($domtree->createElement('description','Payable VAT'));

      /* get the xml printed */
      $xml =  $domtree->saveXML();

      return $xml;
}
所以,上面的页面处理返回消息。实际的XML生成函数位于下页

functions.php

<form method="post" id="invoiceform">
<? /* Populate the form with input later on. For now, the XML data is hardcoded in PHP */ ?>
<button type="submit">Submit form</button>
</form>


//Submit the form:
$('#invoiceform').on('submit', function(e) { //use on if jQuery 1.7+
    e.preventDefault();  //prevent form from submitting
    e.stopImmediatePropagation(); //prevent double.

    //Show the loading message.
    $form = $(this);

     // Use Ajax to submit form data
        $.ajax({
            url: '/api/invoice/invoice_converter',
            type: 'POST',
            data: $form.serialize(),
            dataType: "json",
            success: function(data) {

                            console.log(data);

                if (data.result == 'success') {
                //Success
                $(".status").html(data.message);

            } else {
                $(".status").html(data.message);
           }
        },
        error: function(data) {
            console.log("Something went wrong!");
            console.log(data);

        }
    });

return false;

});
$invoice = new Invoice;
if($_POST)
{
    $convertInvoice = $invoice->convertInvoice();

    if($convertInvoice == 1){
              $error = "Error: Error message goes here.";
              $stop = true;
    }
    if($stop){
        $result = array("result" => "error","message" => $error);
    }else{

        $result = array("result" => "success","message" => $convertInvoice);

    }

}
header('Content-type: application/json');
echo json_encode($result);
function convertInvoice(){

    /* create a dom document with encoding utf8 */
      $domtree = new DOMDocument('1.0', 'UTF-8');

      /* create the root element of the xml tree */
      $xmlRoot = $domtree->createElement("xml");
      /* append it to the document created */
      $xmlRoot = $domtree->appendChild($xmlRoot);

      $currentTrack = $domtree->createElement("track");
      $currentTrack = $xmlRoot->appendChild($currentTrack);

      /* you should enclose the following two lines in a cicle */
      $currentTrack->appendChild($domtree->createElement('charge','letter'));
      $currentTrack->appendChild($domtree->createElement('description','Payable cover letters'));

      $currentTrack->appendChild($domtree->createElement('charge','vat'));
      $currentTrack->appendChild($domtree->createElement('description','Payable VAT'));

      /* get the xml printed */
      $xml =  $domtree->saveXML();

      return $xml;
}
console.log中从上面返回的数据如下:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
  <track>
    <charge>letter</ charge >
    <description>Payable cover letters</ description >
    <charge>vat</ charge >
    <description>Payable VAT</ description >
   </track>
</xml>

信
应付求职信
增值税
应付增值税

这是正确的,但是,我希望能够将上述内容“保存”到XML文件中,并使其可供用户下载。

您需要创建一个可写文件并将$XML写入文件。
这段代码创建track.xml,并插入下载到文档的链接

使用
fopen
以写入(如果不存在则创建)模式打开文件,然后使用
fwrite
将内容写入文件,然后使用
fclose
将其关闭。文件必须放在公共目录中,以确保web服务器可以访问该文件

//打开文件并写入内容
$file=fopen('track.xml','w+');
fwrite($file,$xml);
fclose($文件);
//生成下载链接
回声';
注意:您需要确保输出目录可由PHP通过以下方式写入:

chown www-data:www-data/var/www/path/to/output
chmod 775/var/www/path/to/output
您可以查看以编写以及用户下载