Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Nov 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    XSLT Combobox Variable Passing

    Hi,
    I am trying to create a variable that is displayed to the user after a combobox selection is made. I am using PHP to transform the data and that is all working but when the user selects an option it displays their results but, does not keep the selected option in the combobox. I would also like to display it as a title so the user knows where they are at. Here is the xsl that I have so far. I think my main problem is when I do the transformation after the selection, I am returning a dataset and I don't know how to use PHP inside XSLT. Being able to code I would really like to figure out how to pass variables between PHP and XSL.
    Any help would be great! Thanks

    Code:
    <div id="NavID">
    <form id="frmID" action="/index.php" method="get"> 
    <fieldset>
    
    <select id="choice" name="choice" onchange="this.form.submit()">
    
      <option value="$cmbSelect"></option>
      <option value="5">Drugs</option>
      <option value="7">Weapons</option>
      <option value="9">Sex</option>
    </select>
    
    //How do I get a variable to hold a value between transformations?
    <xsl:variable name="cmbSelect">
    <xsl:value-of select="@choice" />
    </xsl:variable>
    
    <input type="hidden" id="page" name="page" value="suppliers" />
     
     </fieldset>
    </form>
    </div><!--End Nav-->

  • #2
    New Coder
    Join Date
    Nov 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Somewhat Solved

    I ended up using a workaround. Using Xpath I selected one of the node's children as so.

    Code:
    <h2><xsl:value-of select="/root/tblXXX/fldXXX[position()=1]"/></h2>
    Of course IE didn't follow the standard and uses 0 as the index. However, I am to the point where I don't care what IE does.

    I can pass the variable through the GET and then after the transformation I pluck a particular value that was created from the database.

    I would still like some insight from others on how they pass variables between their controller and xsl stylesheets.....

  • #3
    New Coder
    Join Date
    Jul 2009
    Posts
    37
    Thanks
    1
    Thanked 1 Time in 1 Post
    If you are processing your page each time via post you could pass your selected option as a $_POST variable and use:

    http://us.php.net/manual/en/xsltproc...tparameter.php


    basically in your xsl define a parameter
    Code:
    <xsl:param name="selectedOption">No Value</xsl:param>
    then after your post set:

    PHP Code:
    $proc = new XSLTProcessor;
    $proc->importStyleSheet($xsl); // attach the xsl rules
    $proc->setParameter('''selectedOption'$_POST['selectedOption']); 

  • Users who have thanked RossMcCaughrain for this post:

    rizzo89 (07-30-2009)

  • #4
    New Coder
    Join Date
    Nov 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Thanks for the tip, I think that put me on the right track. Still not working though. I have verified the variable is set to the correct value while processing.... But I get this error:

    Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: runtime error: file /home/xxxx/public_html/xxxx/stylesheets/suppliers.xsl line 39 element param in /home/xxxx/public_html/xxxx/index.php on line 534

    Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: Unexpected XSLT element 'param'. in /home/xxxx/public_html/xxxx/index.php on line 534

    Code:
    <xsl:param name="selectedOption" />
    Code:
    <h3>
    <xsl:value-of select="$selectedOption" />
    </h3>
    PHP Code:
    $xslDoc = new DOMDocument();
    $xslDoc->load($_SERVER['DOCUMENT_ROOT'] . '/stylesheets/' $xslStyle '.xsl');
    $proc = new XSLTProcessor();
    $proc->setParameter('''selectedOption'$productType);
    $proc->importStylesheet($xslDoc);


    if (
    $newDoc == null)
    {
        echo 
    $proc->transformToXML($doc);

    }
    else
    {
        echo 
    $proc->transformToXML($newDoc);


  • #5
    New Coder
    Join Date
    Nov 2008
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I needed to put the parameter at the top of the stylesheet!!!


    Code:
    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:param name="selectedOption" select="defaultstring"></xsl:param>
    <xsl:include href="constant/mainTemplate.xsl" />
    <xsl:output method="xml" omit-xml-declaration = "yes" indent="yes"/>
    
    <xsl:template match="/">

  • #6
    New Coder
    Join Date
    Jul 2009
    Posts
    37
    Thanks
    1
    Thanked 1 Time in 1 Post
    Lol. Yeah that one has caught me out before as well!

    Now to select your option i use

    Code:
    <?xml version="1.0">
    <root>
        <options>
            <option>
                <value>Value 1</value
                <name>Name</name>
            </option>
            .....
        </options>
    </root>
    Code:
    <xsl:for-each select="//options">
        <xsl:choose>
            <xsl:when test="./option/value = $selectedOption">
                <option value="{./option/value}" selected="selected">
                    <xsl:value-of select="./option/name" />
                </option>
            </xsl:when>
    	<xsl:otherwise>
    		<option value="{./option/value}">
                        <xsl:value-of select="./option/name" />
                    </option>
    	</xsl:otherwise>
        </xsl:choose>
    </xsl:for-each>

    Anyway glad you got it sorted!


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •