To solve a tricky wiki search problem we've implemented a Javascript solution in a PHP file.
Unfortunately, I have two problems. One is that the function is not being called and secondly I see a syntax error in the Error Console that I cannot solve. First, here is the code within the PHP file:
$htmlOut .= <<<ENDOFBLOCK
<script language="javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 1.0 ' + text;
if (platform2) text = 'Applies to=Platform 2.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</script>
ENDOFBLOCK
;
So, the first problem is that I see appendAndSubmit is not defined in the Error Console.
The second problem is the syntax error. The generated HTML source is:
<p><script language="javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 1.0 ' + text;
if (platform2) text = 'Applies to=Platform 2.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</p>
</script><div align="center" style="background-color:transparent"><form name="searchbox" id="searchbox" class="searchbox" action="/wiki/index.php?title=Special:Search"><input class="searchboxInput" name="search" type="text" value="" size="50" /><br /><input type="checkbox" name="1" value=""Applies to=Platform 1.0"" id="p1" /> <label for="">Platform 1.0</label><input type="checkbox" name="2" value=""Applies to=Platform 2.0"" id="p2" /> <label for="">Platform 2.0</label><br /><input type="submit" name="fulltext" class="searchboxSearchButton" value="Go!" onClick="appendAndSubmit();" /></div></form>
Note the </p>
occurs before </script>
, whereas <p>
occurs before <script>
.
Can anyone tell me please what I'm doing wrong?
The call to the appendAndSubmit function is here:
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'searchboxSearchButton',
'value' => 'Go!',
'onClick' => 'appendAndSubmit();'
)
);
Complete method:
public function getSearchPlatform() {
// Use button label fallbacks
global $wgContLang;
// Use button label fallbacks
if ( !$this->mButtonLabel ) {
$this->mButtonLabel = wfMsgHtml( 'tryexact' );
}
if ( !$this->mSearchButtonLabel ) {
$this->mSearchButtonLabel = wfMsgHtml( 'searchfulltext' );
}
$htmlOut .= <<<ENDOFBLOCK
<script type="text/javascript">
function appendAndSubmit(){
var platform1 = document.getElementById( 'p1').checked;
var platform2 = document.getElementById( 'p2').checked;
var text = document.getElementById('search').value;
if (platform1) text = 'Applies to=Platform 3.0 ' + text;
if (platform2) text = 'Applies to=Platform 4.0 ' + text;
alert( text);
document.getElementById('search').value = text;
document.forms['searchform'].submit();}
</script>
ENDOFBLOCK
;
// Build HTML
$htmlOut .= Xml::openElement( 'div',
array(
'align' => 'center',
'style' => 'background-color:' . $this->mBGColor
)
);
$htmlOut .= Xml::openElement( 'form',
array(
'name' => 'searchbox',
'id' => 'searchbox',
'class' => 'searchbox',
'action' => SpecialPage::getTitleFor( 'Search' )->escapeLocalUrl(),
)
);
$htmlOut .= Xml::element( 'input',
array(
'class' => 'searchboxInput',
'name' => 'search',
'type' => 'text',
'value' => $this->mDefaultText,
'size' => $this->mWidth,
)
);
$htmlOut .= $this->mBR;
// Checkbox
$htmlOut .= Xml::element( 'input',
array(
'type' => 'checkbox',
'name' => '1',
'value' => '"Applies to=Platform 1.0"',
'id' => 'p1'
)
);
// Label
$htmlOut .= ' ' . Xml::label( 'Platform 2.0' );
// Checkbox
$htmlOut .= Xml::element( 'input',
array(
'type' => 'checkbox',
'name' => '2',
'value' => '"Applies to=Platform 2.0"',
'id' => 'p2'
)
);
// Label
$htmlOut .= ' ' . Xml::label( 'Platform 2.0' );
// Line break
$htmlOut .= $this->mBR;
$htmlOut .= Xml::element( 'input',
array(
'type' => 'submit',
'name' => 'fulltext',
'class' => 'searchboxSearchButton',
'value' => 'Go!',
'onClick' => 'appendAndSubmit();'
)
);
// Hidden fulltext param for IE (bug 17161)
if( $type == 'fulltext' ) {
$htmlOut .= Xml::hidden( 'fulltext', 'Search' );
}
$htmlOut .= Xml::closeElement( 'div' );
$htmlOut .= Xml::closeElement( 'form' );
// Return HTML
return $htmlOut;
}