doutan1857 2014-01-24 06:37
浏览 43
已采纳

如何通过php / javascript保存javascript字符串中包含的xml

I have xml page contents in a javascript string. I need to save this into my.xml in the server side using php. But i can't seem to pass the xml-content-string value via POST method. I tried file_get_contents and curl but figured out that's not what I want since I have my xml content in string format and not in a file. Can anyone help me to save this string in javascript as proper xml content.

Thanks in advance

Pre

I am attaching my code below

Page1.php

  <form name = "frm_first" method = "POST">
    <input type = "hidden" name = "xml_string">
    ...............


<script type = "text/javascript" language = "javascript">
function getDataXML(){
 xmlString = chart.getDataXML();// This is where I get my xml data into the string
 document.frm_first.xml_string.value = xmlString;
 alert(xml_string); //Good
 document.frm_first.submit();
}
</script>

Page2.php //This is the page to which post is submitted.

    <?php
    echo print_r($_POST);//xml_string vaiable is empty. Other variables are getting displayed.
    $domtree = new DOMDocument('1.0','UTF-8');
    $domtree->loadXML($_POST['xml_string']);// Error :empty string supplied as input
    $domtree->save("my.xml"); //Yes , I have access to the file.
?>

$_POST['xml_string'] is empty is my issue. Other variables are getting passed via POST.

  • 写回答

2条回答 默认 最新

  • dpbdl44228 2014-01-27 12:04
    关注

    Sorry to be answering my own question, but thought that it might be of use to someone else too. I did a if array_key_exists('xml_string', $_POST)

    <?php
    if array-_key_exists('xml_string', $_POST) 
    {
        echo print_r($_POST);
        $domtree = new DOMDocument('1.0','UTF-8');
        $domtree->loadXML($_POST['xml_string']);
        $domtree->save("my.xml"); 
    ?>
    

    and now $_POST variables are coming in properly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型