Javascript 使用用户界面更新mySQL表-PHP
我正在尝试创建一个界面,您可以在其中编辑/添加/删除mySQL数据库的字段。这就是它的视觉效果,我在客户端的所有功能都正常工作 我的问题是:如何将任何编辑/添加/删除传递到服务器端?我将为我的网站添加一个链接。 下面的代码将显示我目前是如何处理表格的Javascript 使用用户界面更新mySQL表-PHP,javascript,php,html,mysql,Javascript,Php,Html,Mysql,我正在尝试创建一个界面,您可以在其中编辑/添加/删除mySQL数据库的字段。这就是它的视觉效果,我在客户端的所有功能都正常工作 我的问题是:如何将任何编辑/添加/删除传递到服务器端?我将为我的网站添加一个链接。 下面的代码将显示我目前是如何处理表格的 <?php $servername = "localhost"; $username = "lalalal"; $password = "lalalal"; $li
<?php
$servername = "localhost";
$username = "lalalal";
$password = "lalalal";
$link = mysqli_connect("localhost", "lalala", "lalala", "lalala");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$sqlStart = "SELECT `Name`, `EXT`, `Returning Time`, `Returning Date`, `Out`, `Reset`, `Booked` FROM `lalala`";
if($result = mysqli_query($link, $sqlStart)){
if(mysqli_num_rows($result) > 0){
echo "<table id = contactTable>";
echo "<tr id = row1>";
echo "<th id = sortTable onclick=sortTable(0)>Name ↕</th>";
echo "<th style = width:100px;>EXT</th>";
echo "<th style = width:300px;>Returning Time</th>";
echo "<th style = width:300px;>Returning Date</th>";
echo "<th style = width:70px;>Out</th>";
echo "<th style = width:100px;>Reset</th>";
echo "<th style = width:600px;>Booked</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
$currentCheck = $row['Out'];
if ($currentCheck == 0) {
echo "<tr>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
if ($currentTime == 0) {
echo "<td> <form> <input type = 'time', id = 'timePickChange'> </form> </td>";
} else {
echo "<td> <form> <input type = 'time', id = 'timePickChange' value =" . $currentTime . "> </form> </td>";
}
$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = 'datePickChange' value =" . $currentDate . "> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)'> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
} else if ($currentCheck == 1) {
echo "<tr style = 'background-color: #E2E9FD'>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['EXT'] . "</td>";
$currentTime = $row['Returning Time'];
echo "<td> <form> <input type = 'time', id = timePickChange disabled> </form> </td>";
$currentDate = $row['Returning Date'];
echo "<td> <form> <input type = 'date', id = datePickChange disabled> </form> </td>";
echo "<td> <form onclick = 'checkIfOutRow(this)'> <input type = 'checkbox', onclick = 'checkIfOutValue(this)' checked> </form> </td>";
echo "<td> <button onclick = 'clearForm(this)', id = buttonClear>Reset</button> </td>";
echo "<td> <textarea rows = '1', cols = '60'> </textarea> </td>";
}
echo "</tr>";
}
echo "</table>";
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sqlStart. " . mysqli_error($link);
}
?>
根据您的数据验证模型,您可能希望在将输入值发布到后端之前控制客户端的输入值
好了,你已经在客户端添加/编辑/删除你的联系人了,所以如果我理解正确的话,当你的用户点击编辑/删除并确认时,这将是对用户在浏览器中所做操作的确认,这实际上不会有太大的改变,除此之外,你可能需要专用的按钮/行(或任何其他可绑定事件)
对于这些操作,您可以进行批量删除/编辑,这可以通过在JS中过滤出所有修改/删除的数据并以字符串化数组的形式将其发送到后端PHP来轻松完成。
对于插入操作,您可以通过执行POST操作,在将它们添加到表的同时提交它们
可以用这样的方法来实现:
$.ajax({
method: "PUT",
url: "some.php",
data: JSON.stringify(myUpdatedDataInAnArray)
// you might need to stringify your array to ensure format ?
})
.done(function( msg ) {
alert( "Data Updated: " + msg );
});
在后端php中,您可以使用类似以下内容来侦听POST/PUT/DELETE方法:
if (isset($_POST['add'])){
do your thing
}
if (isset($_PUT['updated'])){
//Since you're sending a stringified array, you must parse it with
$myArray = json_decode($_PUT['updated']);
do your thing
}
if (isset($_DELETE['deleted'])){
do your thing
}
我之所以说Ajax,是因为使用传统的POST/PUT/DELETE表单会刷新页面
以下是一些有用的参考资料:
- JS和
- PHP:和
根据您的数据验证模型,您可能希望在将输入值发布到后端之前控制客户端的输入值
好了,你已经在客户端添加/编辑/删除你的联系人了,所以如果我理解正确的话,当你的用户点击编辑/删除并确认时,这将是对用户在浏览器中所做操作的确认,这实际上不会有太大的改变,除此之外,你可能需要专用的按钮/行(或任何其他可绑定事件)
对于这些操作,您可以进行批量删除/编辑,这可以通过在JS中过滤出所有修改/删除的数据并以字符串化数组的形式将其发送到后端PHP来轻松完成。
对于插入操作,您可以通过执行POST操作,在将它们添加到表的同时提交它们
可以用这样的方法来实现:
$.ajax({
method: "PUT",
url: "some.php",
data: JSON.stringify(myUpdatedDataInAnArray)
// you might need to stringify your array to ensure format ?
})
.done(function( msg ) {
alert( "Data Updated: " + msg );
});
在后端php中,您可以使用类似以下内容来侦听POST/PUT/DELETE方法:
if (isset($_POST['add'])){
do your thing
}
if (isset($_PUT['updated'])){
//Since you're sending a stringified array, you must parse it with
$myArray = json_decode($_PUT['updated']);
do your thing
}
if (isset($_DELETE['deleted'])){
do your thing
}
我之所以说Ajax,是因为使用传统的POST/PUT/DELETE表单会刷新页面
以下是一些有用的参考资料:
- JS和
- PHP:和