dongyou2714 2012-05-21 12:25 采纳率: 0%
浏览 63
已采纳

PHP函数未在生成的HTML中定义错误和错误的语法错误</ p>

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;
}
  • 写回答

2条回答 默认 最新

  • duankuiyuant3940 2012-05-23 06:43
    关注

    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;
        }   
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab