【发布时间】:2017-10-08 15:49:56
【问题描述】:
我在 update.php 中有这段代码:
public function update($segment,$id){
$stmt = $this->conn->prepare("UPDATE `segments` SET `segment` = '$segment' WHERE `id` = '$id'") or die($this->conn->error);
if($stmt->execute()){
$stmt->close();
$this->conn->close();
return true;
}
}
在 home.php 我有这个:
<table class = "table table-bordered table-responsive ">
<thead>
<th>Segment</th>
<th>Action</th>
</thead>
<tbody>
<?php
require 'class.php';
$conn = new db_class();
$read = $conn->read();
while($fetch = $read->fetch_array()){
?>
<tr>
<td contenteditable="true"><?php echo $fetch['segment']?></td>
<form action="activate.php" method="post" name="segment">
<textarea style="display: none;" name="segment" id="markup"></textarea>
<input type="hidden" name="id" value="<?php echo $fetch['id'] ;?>">
<td><center><button class = "btn btn-default" name="update"><a href=""></a><span class = "glyphicon glyphicon-edit"></span> Update</button> | <button class = "btn btn-success" type="submit" name="activate"><span class = "glyphicon glyphicon-check"></span> Activate</button> | <button class = "btn btn-danger" name="deactivate"><a href=""></a><span class = "glyphicon glyphicon-folder-close"></span> deactivate</button></center></td>
</form>
</tr>
<?php
}
?>
</tbody>
</table>
我将其设置为 contenteditable="true" 以直接从同一页面更新,
在激活时我这样做了:
<?php
require_once 'class.php';
if(ISSET($_POST['update'])){
$segment =$_POST['segment'];
$id = $_POST['id'];
$conn = new db_class();
$conn->activate($segment, $id);
echo '
<script>alert("Updated Successfully")</script>;
';
}
?>
所以在 js 中我尝试了这个:
$('#work_form').submit(function(){
// Update the DOM
var block_1 = $('#block_1', '#work_form');
block_1.find('input').each(function(i,e){
el = $(e);
el.attr('value', el.val());
});
// Snatch the markup
var markup = block_1.html();
// Place it into the textarea
$('#markup', '#work_form').html( markup );
// Move on
return true;
});
当我尝试更新时,我确实回显了查询以查看我得到了这个:
UPDATE `segments` SET `segment` = '' WHERE `id` = '5'
任何帮助都会很棒,非常感谢你
【问题讨论】:
-
在您的 HTML 中,我看不到任何名称为段的输入字段。这就是为什么你会得到这个未定义的索引。
-
我不需要任何输入字段,我只想从同一页面进行编辑,我只是使 td 标签 contenteditable="true" 这样我可以更改值,但是如何将其保存在数据库中我点击更新@DineshPatra
-
明白这一点,当你提交表单时,只会提交来自input、textarea、select标签的数据。表单元素列表w3schools.com/html/html_form_elements.asp。虽然您将 td 设置为可编辑的内容,但它不会被提交。
-
但是要做到这一点,你可以通过ajax提交数据,或者你可以创建一个显示为none的textare。然后在 formsubmit 上,通过 javascript,将 td 标签的 innerHTML 复制到 textarea。将td标签命名为segment,将textarea命名为segment。
-
我对javascript并不特别,你能告诉我怎么做吗?请?谢谢你@DineshPatra