简体   繁体   中英

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.

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:

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

withdocument.getElementById("taskID").value

always returns me "", so, my PHP request always returns me an empty result. What I am doing wrong? How must I get value "taskID" correctly for my PHP request?

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

was right. I put my code, which the get comments from the server, in window.onload event. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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