簡體   English   中英

無法獲取php請求的html元素值

[英]Trouble with getting value of html element for php request

我正在嘗試使用參數,從頁面中HTML元素獲取的參數值來進行一些PHP服務器請求。

這是頁面代碼:

<body>
    <div style="margin: 10px 0 20px 0">
        <header id="formTitle"></header>
    </div>

    <div style="display:grid; grid-template-columns: 100px 0.4fr 100px 0.4fr; grid-row-gap: 10px;">
        <div class="gridCell"><header>ID</header></div>
        <div class="gridCell"><input id="taskID"></div>
        <div class="gridCell"><header>Date creation</header></div>
        <div class="gridCell"><input id="dateCreation"></div>
        <div class="gridCell"><header>Title</header></div>
        <div class="gridCell"><input id="title"></div>
        <div class="gridCell"><header>Status</header></div>
        <div class="gridCell"><input id="status"></div>
        <div class="gridCell"><header>Creator</header></div>
        <div class="gridCell"><input id="creator"></div>
        <div class="gridCell"><header>Responsible</header></div>
        <div class="gridCell"><input id="responsible"></div>
        <div class="gridCell"><header>Date start</header></div>
        <div class="gridCell"><input id="dateStart"></div>
        <div class="gridCell"><header>Date finish</header></div>
        <div class="gridCell"><input id="dateFinish"></div>    
    </div>

    <div style="margin: 15px 0 5px 0"><header>Description</header></div>
    <div><textarea id="description" style="width: 100%; margin: 0 0 0 0" rows="5"></textarea></div>

    <div id='commentsTree'>
        <script>
            document.getElementById('commentsTree').innerHTML = getCommentsTree();
        </script>    
    </div>

    <div style="text-align: right; margin: 15px 0 0 0"><button onclick="writeTaskData()">Save</button></div>

</body>

我在使用getCommentsTree();函數時遇到麻煩,這里是:

function getCommentsTree() {

    var xhr = new XMLHttpRequest();
    xhr.open('GET', 'http://localhost/testapp/php/comments/getCommentsTree.php?ownerID=' + document.getElementById("taskID").value + '&ownerType=task', false);
    xhr.send();

    if (xhr.status != 200) {
        alert( xhr.status + ': ' + xhr.statusText ); 
    }
    else {
        if(xhr.response != false){
            return xhr.response;
        }
        else{
            return "**** you.";    
        }    
    }

}

麻煩在這里:

沒關系,什么值具有ID為“ taskID”的元素,行

withdocument.getElementById(“ taskID”)。value

總是返回我“”,因此,我的PHP請求總是返回一個空結果。 我做錯了什么? 如何為我的PHP請求正確獲取值“ taskID”?

我知道了。 撰寫此評論:

在調用getCommentsTree時,在輸入元素中沒有值,該值在文檔解析期間被調用

是正確的。 我將我的代碼(從服務器獲取注釋)放在window.onload事件中。 像那樣:

var windowTask = window.open("http://localhost/testapp/site/windows/formTask.html", "taskForm");
                    windowTask.onload = function(){
                        windowTask.document.getElementById("formTitle").innerText = "Task " + selectedRow.taskID;
                        windowTask.document.getElementById("taskID").value = selectedRow.taskID;
                        windowTask.document.getElementById("title").value = selectedRow.title;
                        windowTask.document.getElementById("status").value = selectedRow.status;
                        windowTask.document.getElementById("creator").value = selectedRow.creator;
                        windowTask.document.getElementById("responsible").value = selectedRow.responsible;
                        windowTask.document.getElementById("description").value = selectedRow.description;
                        windowTask.document.getElementById("dateCreation").value = selectedRow.dateCreation;
                        windowTask.document.getElementById("dateStart").value = selectedRow.dateStart;
                        windowTask.document.getElementById("dateFinish").value = selectedRow.dateFinish;
                        windowTask.document.getElementById('commentsTree').innerHTML = getCommentsTree(selectedRow.taskID, 'task');
                    }

並在getCommentsTree()函數中進行一些更改。 這里是:

function getCommentsTree(ownerID, ownerType) {

    var xhr = new XMLHttpRequest();
    debugger;
    xhr.open('GET', 'http://localhost/testapp/php/comments/getCommentsTree.php?ownerID=' + ownerID + '&ownerType=' + ownerType, false);
    xhr.send();

    if (xhr.status != 200) {
        alert( xhr.status + ': ' + xhr.statusText ); 
    }
    else {
        if(xhr.response != false){
            //let commentsTree = JSON.parse(xhr.response);
            //debugger;
            return xhr.response;
        }
        else{
            return "**** you.";    
        }    
    }

}

現在工作正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM