Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用函数将DOMDocument从PHP传输到Javascript_Php_Javascript_Domdocument - Fatal编程技术网

使用函数将DOMDocument从PHP传输到Javascript

使用函数将DOMDocument从PHP传输到Javascript,php,javascript,domdocument,Php,Javascript,Domdocument,我有一个创建DOMDocument XML文件的PHP函数,我需要将DOMDocument转换成Javascript,我考虑过使用 PHP中的函数返回DOMDocument,这是PHP函数 function coursexml($cc, $type){ $xmlfile = new DOMDocument(); if (@$xmlfile->load("books.xml") === false || $cc == "" || $type == "") {

我有一个创建DOMDocument XML文件的PHP函数,我需要将DOMDocument转换成Javascript,我考虑过使用

PHP中的函数返回DOMDocument,这是PHP函数

function coursexml($cc, $type){
    $xmlfile = new DOMDocument();

    if (@$xmlfile->load("books.xml") === false || $cc == "" || $type == "") {

        header('location:/assignment/errors/500');
        exit;
    }
    $string = "";

    $xpath = new DOMXPath($xmlfile);
    $nodes = $xpath->query("/bookcollection/items/item[courses/course='$cc']");
    $x = 0;
    foreach( $nodes as $n ) {

        $id[$x] = $n->getAttribute("id");

        $titles = $n->getElementsByTagName( "title" );
        $title[$x] = $titles->item(0)->nodeValue;
        $title[$x] = str_replace(" /", "", $title[$x]);
        $title[$x] = str_replace(".", "", $title[$x]);

        $isbns = $n->getElementsByTagName( "isbn" );
        $isbn[$x] = $isbns->item(0)->nodeValue;

        $bcs = $n->getElementsByTagName( "borrowedcount" );
        $borrowedcount[$x] = $bcs->item(0)->nodeValue;

        if ($string != "") $string = $string . ", ";

        $string = $string . $x . "=>" . $borrowedcount[$x];
        $x++;
    }

    if ($x == 0) header('location:/assignment/errors/501');
    $my_array = eval("return array({$string});");
    asort($my_array);

    $coursexml = new DOMDocument('1.0', 'utf-8');
    $coursexml->formatOutput = true;
    $node = $coursexml->createElement('result');
    $coursexml->appendChild($node);
    $root = $coursexml->getElementsByTagName("result");
    foreach ($root as $r) {
        $node = $coursexml->createElement('course', "$cc");
        $r->appendChild($node);
        $node = $coursexml->createElement('books');
        $r->appendChild($node);
        $books = $coursexml->getElementsByTagName("books");
        foreach ($books as $b) {
            foreach ($my_array as $counter => $bc) {
                $bnode = $coursexml->createElement('book');
                $bnode = $b->appendChild($bnode);
                $bnode->setAttribute('id', "$id[$counter]");
                $bnode->setAttribute('title', "$title[$counter]");
                $bnode->setAttribute('isbn', "$isbn[$counter]");
                $bnode->setAttribute('borrowedcount', "$borrowedcount[$counter]");
            }
        }
    }   
    return $coursexml;
}

因此,我要做的是用Javascript调用函数,并返回DOMDocument。

您只需将此函数放到URL中(例如,将其放在独立文件中?这取决于您),然后通过AJAX从客户端调用它。有关拨打此类电话的详细信息,请参考

编辑: 尝试创建一个简单的PHP文件,其中包含并调用您拥有的函数。从您目前描述的情况来看,它可能看起来像

 <?php 
    include("functions.php");
    print coursexml($cc, $type);
试试下面的方法

<?php include('coursexml.php'); ?> 
<script> 
    var xml = <?php $xml = coursexml("CC140", "xmlfile"); 
                    echo json_encode($xml->saveXML()); ?>; 
    document.write("output" + xml);
    var xmlDoc = (new DOMParser()).parseFromString(xml, 'text/xml');
</script>

var xml=saveXML());?>;
document.write(“输出”+xml);
var xmlDoc=(new DOMParser()).parseFromString(xml,'text/xml');

我一直在看$.ajax({url:test.html”,context:document.body,success:function(){$(this.addClass(“done”);});但是我不确定如何格式化它,如何调用我想要的函数,以及如何处理返回。首先,您需要将上述函数放入一个php文件中,例如xml.php,以便在浏览文件时(例如)显示xml。这取决于您如何设置应用程序。然后,使用上面引用的函数调用该URL。我在set functions.php中有许多函数,它需要在自己的php文件中吗?因为输出会像$xml=coursexml($cc,$type)那样输出它$string=htmlspecialchars($xml->saveXML());工作?你可以让xml.php包含function.php文件,然后对它进行函数调用,比如
你把我弄丢了,这与Javascript有什么关系?当JAVScription调用时,如何将变量传递给函数当源代码中显示源代码中的输出时,它们似乎是错误的,因为屏幕上没有显示任何内容使用xml作为xml类型似乎有问题,这是我现在拥有的代码var xml=saveXML();?>;books=xml.getElementsByTagName('book')document.write(“”);文件。填写(“”);对于(var i=0;i@CraigWestonxml目前是一个字符串,如果您想将其作为xml文档,则必须对其进行解析,请参阅更新;i@CraigWeston为什么不在服务器端创建表单呢?
<?php include('coursexml.php'); ?> 
<script> 
    var xml = <?php $xml = coursexml("CC140", "xmlfile"); 
                    echo json_encode($xml->saveXML()); ?>; 
    document.write("output" + xml);
    var xmlDoc = (new DOMParser()).parseFromString(xml, 'text/xml');
</script>