0

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="&quot;Applies to=Platform 1.0&quot;" id="p1" />&nbsp;<label for="">Platform 1.0</label><input type="checkbox" name="2" value="&quot;Applies to=Platform 2.0&quot;" id="p2" />&nbsp;<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 .= '&nbsp;' . 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 .= '&nbsp;' . 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;
}
16
  • What is the syntax error exactly? Commented May 21, 2012 at 12:26
  • 1
    Are you actually outputting the $htmlOut variable somewhere? If so, can you please show us where? Also, show us where you're calling the appendAndSubmit function. Commented May 21, 2012 at 12:27
  • @Bono: syntax error: Note the </p> occurs before </script>, whereas <p> occurs before <script>. Thanks. Commented May 21, 2012 at 12:30
  • And how about the generated form control that has the onclick function call? Commented May 21, 2012 at 12:39
  • And for the syntax error, put the </p> after the </script>. You're basically telling javascript to execute </p>, which is garbage to it. When you nest tags in HTML, you close them in order of last opened to first opened. Commented May 21, 2012 at 12:40

2 Answers 2

0

Fix the issue with the misplaced <p> tag inside the <script> element. It is possible that your syntax error within those <script> tags is preventing the registration of the function, since it is declared in that same block.

UPDATE So it's hard to say, because ultimately the Wiki engine is what's probably calling render() on InputBox. But here's a suggestion that may help. For most of your elements, you're adding them with static methods from the Xml class. Have you tried creating your script tag with Xml::openElement? Then you can put your heredoc declaration of the script's body between those calls. If something is inserting p tags arbitrarily on the Wiki side, perhaps using this method will output it in such a way that this doesn't end up happening. It's worth a shot, because, honestly, I can't see anything in the code you've posted that indicates that paragraph tags should be inserted from your code here.

Sign up to request clarification or add additional context in comments.

9 Comments

I wish I could. That's my question - I don't know why the generated code puts the <p> (or rather the </p>) in the wrong place.
See my most recent comment on the actual question--show us where you're outputting that variable with the script in it, and that should help.
Thanks for the update. I think this is a bit beyond my (I'm still a newbie). I'll try to put the JS in a different file first, and use this as a backup plan.
FWIW, it happens when I remove all the code except: $htmlOut .= <<<ENDOFBLOCK <script type="text/javascript"> </script> ENDOFBLOCK ;!
Awesome, so glad you got it fixed. I would still love to know why you got the extra paragraph tag! Notepad++ shouldn't be the culprit, as it's just a raw text editor and doesn't do anything funky like that. At any rate, good luck, and happy continued coding!
|
0

I managed to get rid of the </p> problem by putting everything between <script> and </script> on one line. This makes me wonder if it is a problem with the editor (Notepad++ on Windows) when I put the file on the Linux server. But I thought Notepad++ could handle that (it is no problem for PHP).

The fact that the script was not being called was actually due to a syntax error, which I found out via a clue in another question: 'search' should have been 'searchbox'.

Once that was resolved the function was called without errors.

In case anyone else wants it, here is the complete working code:

    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('searchboxInput').value;if (platform1 * platform2 >0) text = text + ' "Applies to=Platform 1.0" "Applies to=Platform 2.0"';else if (platform1) text = text + ' "Applies to=Platform 1.0"';else if (platform2) text = text + ' "Applies to=Platform 2.0"';document.getElementById('searchboxInput').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',
                'id' => 'searchboxInput',
                'type' => 'text',
                'value' => $this->mDefaultText,
                'size' => $this->mWidth,
            )
        );

        $htmlOut .= $this->mBR;

        // Checkbox
        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'checkbox',
                'name' => '1',
                'id' => 'p1'
            )
        );
        // Label
        $htmlOut .= '&nbsp;' . Xml::label( 'Platform 1.0' );

        // Checkbox
        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'checkbox',
                'name' => '2',
                'id' => 'p2'
            )
        );
        // Label
        $htmlOut .= '&nbsp;' . Xml::label( 'Platform 2.0' );

        // Line break
        $htmlOut .= $this->mBR;

        $htmlOut .= Xml::element( 'input',
            array(
                'type' => 'submit',
                'name' => 'fulltext',
                'class' => 'searchboxSearchButton',
                'value' => 'search',
                '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;
    }   

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.