PHP function is not defined error and syntax error of misplaced </p> in generated HTML

断了今生、忘了曾经 提交于 2019-12-13 03:55:02

问题


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

回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/10685237/php-function-is-not-defined-error-and-syntax-error-of-misplaced-p-in-generate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!