我正在将此产品保修数据库系统作为一个小型学习方面的项目,作为一个新的Web开发。我想拥有的最后一个更大的工具之一有很多问题。这是每行中的一个按钮,单击后将弹出一个模式弹出窗口,并有一个文本框,其中原始注释设置为该值。当用户完成添加或更改评论后,单击另一个按钮,它将更新(我对此部分充满信心)。
此问题在于检索实际评论。我正在使用网上找到的类似项目作为指导工具。他们使用JSON调用php函数来检索数据并将文本框的值设置为数据。我尝试了这种方法,但似乎没有任何效果。我尝试更改代码,阅读有关JSON的更多信息,并与其他项目一起玩,但没有成功。我还尝试使用onclick方法并在实际按钮侧传递参数,但这也不起作用。当我的意思是它不起作用时,我的意思是控制台日志中有0个错误,因此我不确定在哪里寻找更正。
我确实在删除数据类型:'JSON'时使该函数正常工作,但该框从未设置为该值。
这里有一些代码块供参考。是的,它很杂乱,是的,我知道它很容易受到路口攻击。
在表php文件中,用于为每个产品创建按钮。
if ($row['WarrStatus'] == 'ACTIVE'){
echo '<td><button type="button" class="deleteRow" id="'.$id.'" >Delete</button></td>
<td><button type="button" class="updateRow" id="'.$id.'" >Update</button></td>
<td><button type="button" class="resolveRow" id="'.$id.'" >Resolve</button></td>';
}
这是单击按钮updateRow时的js函数,它将执行
$(".updateRow").click(function() {
var item_id = $(this).attr("id");
$.ajax({
url:"Actions/fetch.php",
method:"POST",
data:{item_id:item_id},
dataType:"json",
success:function(data){
document.getElementById("CommentBox") = data.comments //Comments = the data retireved
modal.style.display = "block"; //Display the modal
}
});
});
这是获取json数据的fetch.php。
if(isset($_POST["item_id"])) {
$query = "SELECT comment FROM inspection.product WHERE TransactionID = '".$_POST["item_id"]."'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
echo json_encode($row);
}
?>
最后,这是情态的。
<div class="modal-content"
<span class="close">×</span>
<h2>Update a Comment</h2>
<p id="CustDetails">You are now editing ...</p>
<form method="POST">
<input type ="text" class="CommentBox" id="CommentBox" value=""></input>
<button class="updateCommentbtn" id="updateCommentbtn" value="">Update</button>
</form>
</div>
</div>
总而言之,我的问题似乎是单击更新按钮时似乎没有通过JSON或任何其他方法检索到的数据,我不确定为什么会这样。任何帮助,将不胜感激!
这里至少有一些问题。
isset($_POST["item_id"])
检查将始终返回false。innerHtml
。由于您正在(大概)正在处理文本并且已经在使用jQuery,因此只需执行$("#CommentBox").text(data.comments);
您的问题是来自Javascript成功函数的这一行
document.getElementById("CommentBox") = data.comments
将此更改为:
document.getElementById("CommentBox").value = data.comments