在 PHP 中格式化 HTML 下拉列表的 XML 响应

我有一个 javascript onChange 函数,它调用一个 PHP 文件来运行,获取 UPS 费率,并返回一个带有费率下拉列表的 HTML 更新。我让这一切工作得很好,但现在我需要根据比较向选项列表添加一个项目。


Java脚本:


function fetch_UPS(el){

    var zip = el;

    if (zip.length !== 5) {

        alert ("Please enter a valid 5-digit US zip code.");

        return false;

    }

    else {

        var cartID = getCookie('cornerstoreid');

        document.getElementById('blackoverlay').style.display = "block";

        document.getElementById('pp_checkout').style.display = "none";

        $.ajax({

            type: 'GET',

            cache: false,

            url: 'fetch_rates.php',

            data: { zip : zip, cartID : cartID },

            success: function(response){

                var droplist = document.getElementById("shipping_field");

                var htmlchange = "<span class=\"cart_opt_label\">Shipping: </span>" + response;

                droplist.innerHTML = htmlchange;

                document.getElementById('blackoverlay').style.display = "none";

                document.getElementById('btn_sub_txt').innerHTML = '<input type="button" id="pp_checkout" value="" onclick="paypal_checkout_submit();" style="display:none;" />Please update shipping to continue';

            }

        });

    }

}

用于返回的 PHP(在返回之前删除所有位)


if ($response == false) {

        throw new Exception ( "Bad data." );

    } else {

        // save request and response to file

        $fw = fopen ( $outputFileName, 'w' );

        fwrite ( $fw, $response );

        fclose ( $fw );

        

        // Load XML file

        $xml = new DOMDocument;

        $xml->load($outputFileName);


        // Load XSL file

        $xsl = new DOMDocument;

        $xsl->load('style.xsl');


        // Configure the transformer

        $proc = new XSLTProcessor;


        // Attach the xsl rules

        $proc->importStyleSheet($xsl);


        echo $proc->transformToXML($xml);

    }

在测试中,我的IF语句只在返回的末尾添加一个字符串,但是如果我尝试select从 XSL 中删除结束标记并将其添加到 PHP 文件中,它会丢弃格式化的返回并且只返回IF陈述。


我不做很多编码,而且由于中间通常有 6 个月到一年的时间,所以我经常落后于当前的编码要求。我确信我缺少一些 XSL 规则,但我已经尝试了我能想到的所有搜索组合,但没有提出任何解决方案。



海绵宝宝撒
浏览 100回答 1
1回答

尚方宝剑之说

我想到了。当我最初删除最后两个选项和结束标记时,我留在了SELECTXSL 样式表中的开始标记中。这导致样式表失败,因为它有一个没有结束标记的开始标记。一旦我SELECT从 XSL 样式表中删除了标记行并对 PHP 进行了以下更改,一切都完美无缺。这是对 PHP 的更改:$openTag = '<select name="shipping_options" id="shipping_options">';$closeTag = '<option value=\'8!!!0.00\'>Please call for freight pricing</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value=\'9!!!0.00\'>Please call for international pricing</option>&nbsp; &nbsp; </select>';....else {&nbsp; &nbsp; &nbsp; &nbsp; // save request and response to file&nbsp; &nbsp; &nbsp; &nbsp; $fw = fopen ( $outputFileName, 'w' );&nbsp; &nbsp; &nbsp; &nbsp; fwrite ( $fw, $response );&nbsp; &nbsp; &nbsp; &nbsp; fclose ( $fw );&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; // Load XML file&nbsp; &nbsp; &nbsp; &nbsp; $xml = new DOMDocument;&nbsp; &nbsp; &nbsp; &nbsp; $xml->load($outputFileName);&nbsp; &nbsp; &nbsp; &nbsp; // Load XSL file&nbsp; &nbsp; &nbsp; &nbsp; $xsl = new DOMDocument;&nbsp; &nbsp; &nbsp; &nbsp; $xsl->load('style.xsl');&nbsp; &nbsp; &nbsp; &nbsp; // Configure the transformer&nbsp; &nbsp; &nbsp; &nbsp; $proc = new XSLTProcessor;&nbsp; &nbsp; &nbsp; &nbsp; // Attach the xsl rules&nbsp; &nbsp; &nbsp; &nbsp; $proc->importStyleSheet($xsl);&nbsp; &nbsp; &nbsp; &nbsp; echo $openTag;&nbsp; &nbsp; &nbsp; &nbsp; echo $proc->transformToXML($xml);&nbsp; &nbsp; &nbsp; &nbsp; if ($to_height > 0 && $to_height <= 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $openTag . 'extra text here' . $closeTag;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $openTag . $closeTag;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript