简体   繁体   English

无法获取php请求的html元素值

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

I am trying to do some PHP server request with parameters, witch values I get from HTML elements in my page. 我正在尝试使用参数,从页面中HTML元素获取的参数值来进行一些PHP服务器请求。

This is the page code: 这是页面代码:

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

I have trouble with function getCommentsTree();, here it is: 我在使用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.";    
        }    
    }

}

here the trouble: 麻烦在这里:

No matters, what value have an element with id "taskID", line 没关系,什么值具有ID为“ taskID”的元素,行

withdocument.getElementById("taskID").value withdocument.getElementById(“ taskID”)。value

always returns me "", so, my PHP request always returns me an empty result. 总是返回我“”,因此,我的PHP请求总是返回一个空结果。 What I am doing wrong? 我做错了什么? How must I get value "taskID" correctly for my PHP request? 如何为我的PHP请求正确获取值“ taskID”?

I got it. 我知道了。 Author this comment: 撰写此评论:

here's no value in the input element at the time you're calling getCommentsTree, that is called during the document parsing 在调用getCommentsTree时,在输入元素中没有值,该值在文档解析期间被调用

was right. 是正确的。 I put my code, which the get comments from the server, in window.onload event. 我将我的代码(从服务器获取注释)放在window.onload事件中。 Like that: 像那样:

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

and make some changes in getCommentsTree() function. 并在getCommentsTree()函数中进行一些更改。 Here it is: 这里是:

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

}

Now it works fine. 现在工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM