0
我試圖使用下面的代碼來填充表和註釋和時間戳。不幸的是,當$ _POST被調用時它似乎總是有一個空值(僅在php中發佈「Post in Error」錯誤)。任何想法爲什麼?
HTML:
<main>
<section id="createComment">
<fieldset>
<legend><strong>Leave A Comment</strong></legend>
<textarea id="commentField" class="text"
name="commentField"></textarea>
<button id="submitCComment" onClick="submitComment()"
value="Submit">Submit</button>
</fieldset>
</section>
<br>
<table id="commentsTbl">
<tr>
<th class="row-datetime">Date Time</th> <th class="row-comment">Comment</th>
</tr>
</table>
</main>
的Javascript:
function submitComment() {
var comment = document.getElementById("commentField").value;
if (comment == "" || comment == null){
alert("Comment cannot be blank");
return false;
} else {
var request = new XMLHttpRequest();
request.onreadystatechange = function() {
if(request.readyState == 4 && request.status == 200) {
var text = request.responseText;
var parser = new DOMParser();
var parsedXML = parser.parseFromString(text, "text/xml");
var datetime = parsedXML.getElementsByTagName("datetime")[0].childNodes[0].nodeValue;
var comment = parsedXML.getElementsByTagName("comment")[0].childNodes[0].nodeValue;
var table = document.getElementById("commentsTbl");
var newRow = table.insertRow(1);
var newCell = newRow.insertCell(0);
newCell.innerHTML = datetime;
newCell = newRow.insertCell(1);
newCell.innerHTML = comment;
}
};
// submit request to the server
request.open("POST", "/PHP/ProcessCommentDateTime.php", true);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
request.send("comment=" + comment);
document.getElementById("commentField").value = "";
}
}
PHP:
<?php
$comment;
$comment = $_POST["commentField"];
date_default_timezone_set('UTC');
$datetime = date("Y-m-d H:i:s");
if($comment == null){
$comment = "Error on Post";
}
//storeComment($comment, $datetime);
echo('<usercomment>' .
'<datetime>' . $datetime . '</datetime>' .
'<comment>' . $comment . '</comment>' .
'</usercomment>');
?>
完美的作品。謝謝! –