[英]How to save html values to MySQL using ajax?
I'm trying to make an HTML table dynamically. 我正在尝试动态制作HTML表。 I'm just testing with sample, but It's not working as well as I expected.
我只是用示例进行测试,但是效果不如预期。 So, I'm asking this question.
所以,我在问这个问题。 Let me show you this simple table, it is MySQL database.
让我向您展示这个简单的表,它是MySQL数据库。 and I create HTML table.
然后创建HTML表。
What I want to do is when I click some node, like a '250', this number will be editable. 我想要做的是,当我单击某个节点(例如“ 250”)时,此数字将是可编辑的。 Edited data will be saved to the MySQL database.
编辑后的数据将保存到MySQL数据库。 So I use the
contenteditable="true"
attribute value, and when this node loses focus, run some JavaScript function that new data will be saved. 因此,我使用
contenteditable="true"
属性值,并且当此节点失去焦点时,运行一些JavaScript函数以保存新数据。
But... It's not working well. 但是...效果不好。 Honestly I don't know what the problem is exactly.
老实说,我不知道问题到底出在哪里。 Google chrome spit up "Uncaught Syntax Error, Unexpected token } ", But I couldn't find what the actual problem is.
Google Chrome吐出“未捕获的语法错误,意外的令牌}”,但我找不到实际的问题。 I'm newbie on this... Could you some guys help me?
我是这个方面的新手...有些人能帮我吗?
sample.html sample.html
<body>
<?php
require_once("db_connect.php");
$sql = "SELECT * FROM test";
$result = $conn->query($sql);
?>
<table border="1" style="border-collapse:collapse;">
<thead>
<tr><th>name</th><th>price</th></tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()){
echo
"<tr>
<td contenteditable='true' onBlur='saveToDatabase(this,'name','$id')'>{$row['name']}</td>
<td contenteditable='true' onBlur='saveToDatabase(this,'price','$id')'>{$row['price']}</td>
</tr>\n";
}
?>
</tbody>
</table>
<script>
function saveToDatabase(editableObj, column, id) {
$.ajax({
url: "saveedit.php",
type: "POST",
data:'column='+column+'&editval='+editableObj.innerHTML+'&id='+id,
success: function(data){
$(editableObj).css("background","#FDFDFD");
}
});
}
</script>
</body>
</html>
saveedit.php saveedit.php
<?php
require_once("db_connect.php");
$result = mysql_query("UPDATE test SET ". $_POST["column"]. " = '".$_POST["editval"]."' WHERE id=".$_POST["id"]);
?>
You made it contenteditable but PHP needs an parameter. 您使它可编辑,但是PHP需要一个参数。 For example
例如
<input type="text" name="parameter">
will send as $_POST['parameter'] but in your code <td>
elements don't have a name. 将以$ _POST ['parameter']的形式发送,但在您的代码中
<td>
元素没有名称。 So PHP doesn't know if there is a parameter or what name is. 因此,PHP不知道是否有参数或名称是什么。
Maybe you can write your own editable function in Javascript like this: 也许您可以像这样用Javascript编写自己的可编辑函数:
function doTextArea(i) {
document.getElementById(i).innerHTML="<input type='text' name='table_value'>ssss.";
}
and your php loop can be like this ( I assume it is for loop): 和您的PHP循环可以是这样的(我假设它是用于循环):
for ($i = 1; $i <= 10; $i++) {
echo
"<tr>
<td><p id=$i ondblclick='doTextArea($i)'> sadasdasdasd</p></td>
</tr>\n";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.