使用php Ajax在数据库中插入动态数据
16lz
2021-01-22
I tried to insert the dynamic data in database through php Ajax. My code is not working in the dynamic format. But, I have done on the static format, it works fine. Please guide me, how to insert the dynamic data in database through php Ajax.
我试图通过php Ajax在数据库中插入动态数据。我的代码不能使用动态格式。但是,我已经完成了静态格式,它运行正常。请指导我,如何通过php Ajax在数据库中插入动态数据。
Please refer the link http://phppot.com/php/php-mysql-inline-editing-using-jquery-ajax/
请参阅链接http://phppot.com/php/php-mysql-inline-editing-using-jquery-ajax/
Please refer my code below.
请参考下面的代码。
//static Value Update using ajax
function showEdit(editableObj) {
$(editableObj).css("background", "#FFF");
}
function saveToDatabase(editableObj, column, id) {
$(editableObj).css("background", "#FFF url(loaderIcon.gif) no-repeat right");
$.ajax({
url: "saveedit.php",
type: "POST",
data: 'column=' + column + '&editval=' + editableObj.innerHTML + '&id=' + id,
success: function (data) {
$(editableObj).css("background", "#FDFDFD");
}
});
}
//Dynamic data store and update in db using ajax
$(document).ready(function () {
var i = 5;
$('#add').click(function () {
i++;
$('#table').append('<tr class="table-row" id="row' + i + '"><td></td><td contenteditable="true" onBlur="saveToDatabase1(this)" onClick="showEdit(this);"><?php echo Name ?></td><td contenteditable="true" onBlur="saveToDatabase1(this)" onClick="showEdit(this);"><?php echo Designation ?></td></tr>');
});
function saveToDatabase1(editableObj1) {
$(editableObj1).css("background", "#FFF url(loaderIcon.gif) no-repeat right");
$.ajax({
url: "saveedit.php",
type: "POST",
/*How to change below code to dynamic data insert in db*/ data: 'column1=' + column + '&editval1=' + editableObj1.innerHTML + '&id=' + id,
success: function (data) {
$(editableObj1).css("background", "#FDFDFD");
}
});
}
});
.current-row {
background-color: #B24926;
color: #000;
}
.current-col {
background-color: #1b1b1b;
color: #000;
}
.tbl-qa {
width: 100%;
font-size: 0.9em;
background-color: #f5f5f5;
}
.tbl-qa th.table-header {
padding: 5px;
text-align: left;
padding: 10px;
}
.tbl-qa .table-row td {
padding: 10px;
background-color: #FDFDFD;
}
tr {
cursor: pointer
}
.selected {
background-color: red;
color: #000;
font-weight: bold
}
button {
margin-top: 10px;
background-color: #eee;
border: 2px solid #00F;
color: #17bb1c;
font-weight: bold;
font-size: 25px;
cursor: pointer
}
.tbl-qa {
counter-reset: serial-number;
}
.tbl-qa td:first-child:before {
counter-increment: serial-number;
content: counter(serial-number);
}
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<table class="tbl-qa" id="table">
<thead>
<tr>
<th class="table-header" width="10%">Emp.No.</th>
<th class="table-header">Name</th>
<th class="table-header">Designation</th>
</tr>
</thead>
<tbody>
<?php
foreach($faq as $k=>$v) {
?>
<tr class="table-row">
<!-- <td><?php echo $k+1; ?></td> -->
<td></td>
<td contenteditable="true" onBlur="saveToDatabase(this,'question','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["question"]; ?
</td>
<td contenteditable="true" onBlur="saveToDatabase(this,'answer','<?php echo $faq[$k]["id"]; ?>')" onClick="showEdit(this);"><?php echo $faq[$k]["answer"]; ?></td>
</tr>
<?php
}
?>
</tbody>
</table>
<button type="button" name="add" id="add" class="btn btn-success">Add More</button>
/* Saveedit.php */
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
$result = $db_handle->executeUpdate("UPDATE php_interview_questions set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE id=".$_POST["id"]);;
?>
更多相关文章
- 试着在我的javascript代码中理解“this”(一件事有效,另一件没有)
- 如何在点击时动态加载/播放/暂停多源HTML5音频?
- 代码点火-如何从控制器返回Json响应
- 小弟初学网页(javascript),看不懂下面的代码。但又要完成任务 各位
- 使用jquery在textarea中的颜色代码
- 为什么我的javascript/jquery代码不能像预期的那样工作?
- 实现浮动广告的代码
- jQuery和AJAX - 使用Ajax添加的对象动态不适用于jQuery函数?
- OnClick事件只获取第一个动态创建的行/ id