所以,我有这个网站,我创建了一些按钮,使表格行中的单元格可编辑: http://imgur.com/tFQMgQG
在用户编辑他想要的任何单元格后,他应该按" Save"按钮,如图所示。现在我想得到他在这些单元格中编写的任何内容并将其发送到PHP文件,其中每个变量都指向用户编辑的单元格将被验证。如果它们最终都经过验证,我会使用这些新值更新SQL数据库。
有没有办法做到这一点?这是我的脚本和一些php:
<script>
var buttons = document.getElementsByClassName("clicker");
var buttonclicked = function(e){
if(e.target.textContent == "Edit")
{
e.target.textContent = "Save";
var id = e.target.id;
var editable_elements = document.querySelectorAll("[contenteditable=false]");
for(var i = (id*7); i < (id*7)+7; i++){
editable_elements[i].setAttribute("contentEditable", true);}
}
else
{
e.target.textContent = "Edit";
var id = e.target.id;
var editable_elements = document.querySelectorAll("[contenteditable=true]");
for(var i = (id*7); i < (id*7)+7; i++){
editable_elements[i].setAttribute("contentEditable", false);}
}
};
for(var j = 0; j < buttons.length; j++)
{
buttons[j].addEventListener('click', buttonclicked);
}
</script>
PHP(以显示我需要将此变量传递给它的原因):
else if($_POST["criteria"] == 3 && (!empty($_POST["g3"]) || !empty($_POST["g4"])))
{
apologize("This can not happen...");
}
if(empty($_POST["g1"]))
$g1 = -1;
else
$g1 = $_POST["g1"];
if(empty($_POST["g2"]))
$g2 = -1;
else
$g2 = $_POST["g2"];
if(empty($_POST["g3"]))
$g3 = -1;
else
$g3 = $_POST["g3"];
if(empty($_POST["g4"]))
$g4 = -1;
else
$g4 = $_POST["g4"];
$subject = strtoupper($_POST["subject"]);
$normal = media($_POST["criteria"], $g1, $g2, $g3, $g4);
query("INSERT INTO portfolio (id, subject, G1, G2, G3, G4, criteria, creditos, normal) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE G1 = VALUES(G1), G2 = VALUES(G2), G3 = VALUES(G3), G4 = VALUES(G4), criteria = VALUES(criteria), creditos = VALUES(creditos), normal = VALUES(normal)", $_SESSION["id"], $subject, $g1, $g2, $g3, $g4, $_POST["criteria"], $_POST["creditos"], $normal);
PS:editable_elements从0到6,包括&#34; g1,g2,g3,g4,条件,信用&#34;和另一个对这个线程不重要的变量(但我也想把它发送到php)
可以修改的HTML部分是:(记住你在这个表中看到的所有变量都是从另一个php文件发送的)
<?php $i = 0;?>
<?php foreach ($rows as $row): ?>
<tr class="d1">
<td><?php echo $row["subject"] ?></td>
<td contenteditable = "false" id = "<?php echo 'g1'.$i; ?>">
<?php
if($row["G1"] != -1)
echo $row["G1"];
else
echo " ";
?>
</td>
<td contenteditable = "false" id = "<?php echo 'g2'.$i; ?>">
<?php
if($row["G2"] != -1)
echo $row["G2"];
else
echo " ";
?>
</td>
<td contenteditable = "false" id = "<?php echo 'g3'.$i; ?>">
<?php
if($row["G3"] != -1)
echo $row["G3"];
else
echo " ";
?>
</td>
<td contenteditable = "false" id = "<?php echo 'g4'.$i; ?>">
<?php
if($row["G4"] != -1)
echo $row["G4"];
else
echo " ";
?>
</td>
<td>
<?php
$round = round($row["normal"],2);
echo $round;
?>
</td>
<td contenteditable = "false" id = "<?php echo 'creditos'.$i; ?>"><?= $row["creditos"] ?></td>
<td contenteditable = "false" id = "<?php echo 'criteria'.$i; ?>"><?php echo $row["criteria"];?></td>
<td><button class = "clicker" id = "<?php echo $i; ?>">Edit</button></td>
</tr>
<?php $i++; ?>
<?php endforeach ?>
答案 0 :(得分:2)
您可以通过2种方法轻松完成此操作
您可以使用ajax将值发送到php文件。
您可以使用脚本创建动态表单,并通过提交发布所有值。