View Full Version : XSLT Combobox Variable Passing

07-10-2009, 05:47 PM
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

<div id="NavID">
<form id="frmID" action="/index.php" method="get">

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

//How do I get a variable to hold a value between transformations?
<xsl:variable name="cmbSelect">
<xsl:value-of select="@choice" />

<input type="hidden" id="page" name="page" value="suppliers" />

</div><!--End Nav-->

07-29-2009, 08:27 PM
I ended up using a workaround. Using Xpath I selected one of the node's children as so.

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

07-30-2009, 12:58 PM
If you are processing your page each time via post you could pass your selected option as a $_POST variable and use:


basically in your xsl define a parameter

<xsl:param name="selectedOption">No Value</xsl:param>

then after your post set:

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

07-30-2009, 08:44 PM
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

<xsl:param name="selectedOption" />

<xsl:value-of select="$selectedOption" />

$xslDoc = new DOMDocument();
$xslDoc->load($_SERVER['DOCUMENT_ROOT'] . '/stylesheets/' . $xslStyle . '.xsl');
$proc = new XSLTProcessor();
$proc->setParameter('', 'selectedOption', $productType);

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

echo $proc->transformToXML($newDoc);

07-31-2009, 05:43 PM
I needed to put the parameter at the top of the stylesheet!!!

<?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="/">

08-05-2009, 12:26 PM
Lol. Yeah that one has caught me out before as well!

Now to select your option i use

<?xml version="1.0">
<value>Value 1</value

<xsl:for-each select="//options">
<xsl:when test="./option/value = $selectedOption">
<option value="{./option/value}" selected="selected">
<xsl:value-of select="./option/name" />
<option value="{./option/value}">
<xsl:value-of select="./option/name" />

Anyway glad you got it sorted! :thumbsup: