dongtan2603 2017-07-20 07:55
浏览 34
已采纳

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

I am having troubles submitting my form using jQuery/AJAX, and returning a success message + the XML file (generated in PHP).

This is what I have now:

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;

});

OK so, above simply submits the form to below page

invoice_converter.php:

$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);

So, above page handles return messages. The actual XML generating function is located in below page

functions.php:

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;
}

The data returned from above in the console.log is this:

<?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>

Which is correct, however, I want to be able to "Save" the above in a XML file, and make it available for the user to download.

  • 写回答

1条回答 默认 最新

  • doumu6941 2017-07-20 08:12
    关注

    You need make a writable file and write $xml to file.
    This code create track.xml and inserts download link to document.

    <?php    
    $file = fopen('track.xml', 'w+');        //Create file
    fwrite($file, $xml);                     //Write xml to file
    fclose($file);                           //Close file    
    echo '<a href="track.xml">Download</a>'; //Make download link
    ?>
    

    Note: track.xml must be writable. You can make it writable by executing: chmod 755 track.xml

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现