未定义PHP函数错误和错误的</p>语法错误在生成的HTML中 [英] PHP function is not defined error and syntax error of misplaced </p> in generated HTML

查看:116
本文介绍了未定义PHP函数错误和错误的</p>语法错误在生成的HTML中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要解决棘手的Wiki搜索问题我们已经在PHP文件中实现了Javascript解决方案.

To solve a tricky wiki search problem we've implemented a Javascript solution in a PHP file.

不幸的是,我有两个问题.其一是未调用该函数,其次,我在错误控制台中看到无法解决的语法错误.首先,这是PHP文件中的代码:

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
    ;

因此,第一个问题是我在错误控制台中看到 appendAndSubmit未定义.

So, the first problem is that I see appendAndSubmit is not defined in the Error Console.

第二个问题是语法错误.生成的HTML源是:

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>

请注意,</p>出现在</script>之前,而<p>出现在<script>之前.

Note the </p> occurs before </script>, whereas <p> occurs before <script>.

有人可以告诉我我在做什么错吗?

Can anyone tell me please what I'm doing wrong?

对appendAndSubmit函数的调用在这里:

The call to the appendAndSubmit function is here:

    $htmlOut .= Xml::element( 'input',
        array(
            'type' => 'submit',
            'name' => 'fulltext',
            'class' => 'searchboxSearchButton',
            'value' => 'Go!',
            'onClick' => 'appendAndSubmit();'
        )
    );

完整方法:

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

推荐答案

通过将<script></script>之间的所有内容都放在一行上,我设法摆脱了</p>问题.当我将文件放在Linux服务器上时,这使我想知道编辑器是否存在问题(Windows上为Notepad ++).但是我认为Notepad ++可以解决这个问题(对于PHP来说这没问题).

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

这篇关于未定义PHP函数错误和错误的&lt;/p&gt;语法错误在生成的HTML中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆