편집 할 때 ajax를 통해 데이터베이스의 테이블을 업데이트해야하는 contenteditable 테이블이 있지만 값을 변경할 때 데이터베이스에는 아무 일도 일어나지 않습니다.
<div id="activities2" class="activities2"> <?
$q2a="SELECT activityid, activitynumber, title, description, leaders, time FROM activities where activities.meetingid='$id' AND activities.unitid='$input2'";
$r2a=mysqli_query($dbc,$q2a) or die(mysqli_error($dbc));
echo "<table class='layouttable'><tr><td>ActivityNumber</td><td>Title</td><td>Description</td><td>Leaders</td><td>Time</td><td>Edit</td>";
while($row2a
=mysqli_fetch_assoc($r2a))
{
echo "
<tr>
<td contenteditable='true' id='activitynumber:" . $row2a['activityid'] . "'>" . $row2a['activitynumber'] . "</td>
<td contenteditable='true' id='title:" . $row2a['activityid'] . "'>" . $row2a['title'] . " </td>
<td contenteditable='true' id='description:" . $row2a['activityid'] . "'>" . $row2a['description'] . " </td>
<td contenteditable='true' id='leaders:" . $row2a['activityid'] . "'>" . $row2a['leaders'] . " </td>
<td contenteditable='true' id='time:" . $row2a['activityid'] . "'>" . $row2a['time'] . " </td>
</tr>.
";
}
echo"</table>"; ?><br><Br></div>
ajax 용 자바 스크립트는 다음과 같습니다.
<script name = 'inlineedit'>
$(document).ready(function(){
$("td[contenteditable=true]").blur(function(){
var msg = $(".alert");
var newvalue = $(this).text();
var field = $(this).attr("id");
$.post("activityupdate.php",field+"="+newvalue,function(d){
var data = JSON.parse(d);
msg.removeClass("hide");
if(data.status == '200'){
msg.addClass("alert-success").removeClass("alert-danger");
}else{
msg.addClass("alert-danger").removeClass("alert-success");
}
msg.text(data.response);
setTimeout(function(){msg.addClass("hide");},3000);//It will add hide class after 3 seconds
});
});
});
</script>
그리고 편집을 수행하는 php 파일은 다음과 같습니다.
<?
$response = NULL;
$status = http_response_code(406);
if(!empty($_POST)){
include "connect_db.php"; //Including Database Settings
foreach($_POST as $key=>$value){
$key = strip_tags(trim($key));
$value = strip_tags(trim($value));
$explode = explode(":",$key);
$activity_id = $explode[1];
$field_name = $explode[0];
if(isset($activity_id)){
$query = "UPDATE activities SET $field_name='{$value}' WHERE activityid='$activity_id'";
$update = mysqli_query($dbc,$query) or die(mysqli_error($dbc)); //Update the activity Table
if($update){
$response = "User Details Updated";
http_response_code(200); //Setting HTTP Code to 200 i.e OK
}else{
$response = "Not Modified";
http_response_code(304); //Setting HTTP Code to 304 i.e Not Modified
}
}else{
$response = "Not Acceptable";
}
}
}
echo json_encode(array(
"status"=>$status,
"response"=>$response
));
?>
코드에서 약간의 변경이 필요합니다.
js에서는 흐림 대신 변경시 사용하십시오.
$("td.editable").on ("change",function(){....
이것은 편집 가능한 필드의 모든 변경에 대해 ajax를 실행합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다