我是怎么把我的Javascript搞砸的


How did I mess up my Javascript?

我把代码搞得一团糟,所以我需要一些帮助。

我得到以下错误:

SyntaxError:预期的表达式,得到了脚本的结尾

这个错误似乎告诉我,我要么有大括号,要么有分号,由于今天的疯狂性质,我似乎找不到它。我正在尝试通过将事件侦听器功能移动到UserInterface.js中的window.onload函数处理程序下,来减少我的Javascript的突兀性;但是,我需要在PHP和onload事件之间传递一个"Fields"数组,所以我想将onload事件保留在:<body onload="OnLoad('.$EncodedFields . ');">';,除非有人能告诉我传递这个变量的更好方法。

不知怎的,在我的干预中,我引发了这个错误。有人知道如何修复这个错误吗?我应该如何正确地将php变量传递给Javascript,以便在onload事件中使用?

UserInterface.php

class UserInterface {
    var $ParentAppInstance;
    function __construct($AppInstance){
        $this->ParentAppInstance = $AppInstance;
        $this->DrawPageHTML();
        $this->DrawDBSetDropdown();
        $this->DrawQueryForm();
    }

    //Override thjis function to change the HTML and PHP of the UI page.
    protected function DrawPageHTML(){
        $DBSet_Num = filter_var($this->ParentAppInstance->CurrentDBSet_Str, FILTER_SANITIZE_NUMBER_INT);
        $CurrDBSet_Obj = $this->ParentAppInstance->DBSets_Arr[$DBSet_Num];
        $EncodedFields = json_encode($CurrDBSet_Obj->GetDBSetFields());
        echo '<body onload="OnLoad('. $EncodedFields .');">';
        echo '
            <div id="DebugOutput"></div>
        </body>

        ';
        //json_encode($CurrDBSet_Obj->GetDBSetFields())
        echo '$AppInstanceData: ' . '<br>';
        echo '--CurrentDBSet_Str: ' . $this->ParentAppInstance->CurrentDBSet_Str;
    }
    protected function DrawDBSetDropdown(){
        echo '<div align="right">';
            echo '<select onchange="SwitchDatabaseSet();" name="DBSetList" form="DBSetSelector" id="DBSetSelector">';
            $i = 0;
            foreach ($this->ParentAppInstance->DBSets_Arr as $DBSet){
                if ($DBSet->DBSetName == $this->ParentAppInstance->CurrentDBSet_Str){
                    echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
                }
            }
            foreach ($this->ParentAppInstance->DBSets_Arr as $DBSet){
                if ($DBSet->DBSetName == $this->ParentAppInstance->CurrentDBSet_Str){/* DO NOTHING. IE. IGNORE IT*/}
                else if ($DBSet->DBSetName == 'DBSet0'){/* DO NOTHING. IE. IGNORE IT*/}
                else{
                    //Add the DBSet to the dropdown list.
                    $i++;
                    echo '<option value="' . $DBSet->DBSetName . '">' . $DBSet->DBSetName . '</option>';
                }
            }
            echo '</select>';
        echo '</div>';
    }
    protected function DrawQueryForm(){
        echo '<form action="search.php" method="post">';
            echo '<div id="QFormBody">';
            $NumActiveQBoxes = $this->ParentAppInstance->Config['ApplicationSettings']['NumberDefaultQueryOptions'];
            for ($i = 0; $i < $NumActiveQBoxes; $i++){
                echo '<div class="QueryBox" name="QBox_' . $i . '">';
                    echo '<select name=Field_' . $i . '">';
                        $DBSet_Num = filter_var($this->ParentAppInstance->CurrentDBSet_Str, FILTER_SANITIZE_NUMBER_INT);
                        $CurrDBSet_Obj = $this->ParentAppInstance->DBSets_Arr[$DBSet_Num];
                        foreach($CurrDBSet_Obj->GetDBSetFields() as $Field){
                            echo '<option>' . $Field . '</option>';
                        }
                    echo '</select>';
                    echo '<input type="text"></input>';
                    echo '<button class= "RMButton" type="button">-</button>';
                echo '</div>';
            }
            echo '<button type="button" id="add" onclick="AddQueryBox();">+</button>';
            echo '<button type="submit" id="submit">SEARCH</button>';
        echo '</Form>';
        $EncodedFields = json_encode($CurrDBSet_Obj->GetDBSetFields());
        echo '<script src=/GLS_DBSearchProject/JavaScript/UserInterface.js></script>';
    }
}

UserInterface.js

var DBSetFields = [];
var NumQBoxes = 3;
function OnLoad(Fields){
    console.log("Alpha");
    console.log(Fields);
    CloneDBSetFields(Fields);
    var RMNodeList = document.getElementsByClassName('RMButton');
    for (var i = 0; i < RMNodeList.length; ++i) {
        console.log(RMNodeList[i]);
        RMNodeList[i].onclick = RemoveQBox;  // Calling myNodeList.item(i) isn't necessary in JavaScript
    }
}
function Fields_FOREACH(ELEMENT, INDEX, ARRAY){
    var FieldOption = document.createElement('option');
    FieldOption.setAttribute('value', ARRAY[INDEX]);
    FieldOption.innerHTML = ARRAY[INDEX];
    this.appendChild(FieldOption);
}
function CloneDBSetFields(Fields){
    console.log("CloneDBSetFields");
    DBSetFields = Fields;

}
function AddQueryBox(){
    NumQBoxes += 1;
    var NewQBox = document.createElement('div');
    NewQBox.setAttribute('class', 'QueryBox');
    //Create and fill Field Selector dropdown "select" element
    var FieldSelector = document.createElement('select');
    FieldSelector.setAttribute('name', 'Field_' + NumQBoxes);
    //foreach element in Fields
    console.log(DBSetFields);
    DBSetFields.forEach(Fields_FOREACH, FieldSelector);
    //Create and fill 
    var QueryText = document.createElement('input');
    QueryText.setAttribute('type', 'text');
    QueryText.setAttribute('name', 'Query_' + NumQBoxes);
    //Create "-" Remove button for removing query lines.
    var RemoveButton = document.createElement('button');
    RemoveButton.innerHTML = "-";
    RemoveButton.setAttribute('type', 'button');
    RemoveButton.setAttribute('class', 'RMButton');
    RemoveButton.addEventListener("click", RemoveQBox);
    //Combine the individual elements into a new query box and insert the new query box into the HTML Document
    NewQBox.appendChild(FieldSelector);
    NewQBox.appendChild(QueryText);
    NewQBox.appendChild(RemoveButton);
    document.getElementById("QFormBody").insertBefore(NewQBox, document.getElementById("add"));
}
function RemoveQBox(e){
    console.log("Remove");
    var RemoveButton = this; //this == e.currentTarget
    console.log(RemoveButton);
    var QBox = RemoveButton.parentNode;
    QBox.remove();
    NumQBoxes -= 1;
}

显然:

body onload="onload('.$EncodedFields.');"

是什么真正导致了这个问题。出于某种原因,我还没有完全弄清楚为什么php串联运算符在这种情况下不起作用。

将此行更改为body onload="OnLoad{$EncodedFields};"或交换单引号和双引号(同时删除句点)也解决了此问题。