具有多行和删除的表


Table with multiple rows and deletions

好的,我有一个输入函数,它允许我向数据库添加项目,并将其显示为一个表。作为表的一部分,我正在尝试添加删除和编辑按钮。

我正在试图找出最好的方法来添加删除和编辑功能。我想编辑,我将不得不使用Javascript。但是,对于删除,我不确定是否应该使用PHP, Javascript或其中的某些组合。

到目前为止,我的代码如下:
<html>
    <header><title>Generic Web App</title></header>
    <body>
        <form action="addculture.php" method="POST">
              <span><input type="text" size="3" name="id" />
              <input type="text" name="culture" />
            <input type="submit" value="add" /></span>
        </form>
<?php
/* VARIABLE NAMES
 *
 * $con = MySQL Connection 
 * $rescult = MySQL Culture Query
 * $cultrow = MySQL Culture Query Rows
 */
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysql_select_db("generic");
$rescult = mysql_query("SELECT * FROM culture order by cult_id");
if (!$rescult) {
    die('Invalid query: ' . mysql());
}
echo "<table><tbody><tr><th>ID</th><th>Culture Name</th>";
while ($cultrow = mysql_fetch_array($rescult)) {
    echo "<tr>" . "<td>" . $cultrow[0] . "</td>" . "<td>" . $cultrow[1] . "</td>" . '<td><button type="button">Del</button></td>' . '<td><button type="button">Edit</button></td>' . "</tr>";
}
echo "</tbody></table>";
?>
    </body>
</html>

目前我有删除和编辑设置为按钮,只是为了可见的参考。当你有这样多个按钮时,最好的处理方法是什么?

如果我的回答太宽泛,我很抱歉,但你的问题也是如此。

编辑和删除都应该使用JavaScript和PHP代码的组合;例如,当用户单击删除按钮时,您可以向服务器发送一个Ajax请求,从DB中删除记录,并在从服务器端调用成功返回后,使用JavaScript从标记中可视化地删除记录。这同样适用于Edit功能。

下面是关于如何使用JQuery执行ajax请求的一个很好的介绍:

http://www.devirtuoso.com/2009/07/beginners-guide-to-using-ajax-with-jquery/

我首先要做的是为按钮添加一个值和名称:

<button type="button" value="$cultrow[0]" name="Delete">Delete</button>
<button type="button" value="$cultrow[0]" name="Edit">Edit</button>

按钮的值将是行id,而名称将是按钮将要执行的操作。接下来我要做的就是用jquery将这些按钮绑定到actions。

$('button').click(function(){
 //determine whether is delete or edit
 //Once you determine what action use the id to make the request
 //if delete prompt to verify if yes, ajax the server with a delete request
 //if edit redirect user to a page that will handle editing of the row edit.php?id=5
});

用于删除行

1 -将新页面命名为del_culture.php

session_start(); 
$id = base64_decode($_GET['id']); 
$con = mysql_connect("localhost", "root", "");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$q = mysql_query("DELETE FROM culture WHERE cult_id = '".$id."' ");
if($q):
   echo "Done"; 
else:
   echo "ERROR";
endif;  

2 -在您的页面中添加以下代码

 while ($cultrow = mysql_fetch_array($rescult)) 
{
echo "<tr>" . "<td>" . $cultrow[0] . "</td>" . "<td>" . $cultrow[1] . "</td>" . '    
<td><a href="del_culture.php?id='.base64_encode($cultrow['cult_id']).'">Delete</a>' . '<td><button type="button">Edit</button></td>' . "</tr>";
}

为编辑行添加编辑链接,如我所做的页面名称edit_cult.php和输入时一样输入数据库中的值然后更新

这是你的javascript

function performAction(action) {
// ASSIGN THE ACTION
var action = action;
// UPDATE THE HIDDEN FIELD
document.getElementById("action").value = action;
switch(action) {
    case "delete":
        //we get an array with every input contained into the form, and the form have an id
        var aryCheck=document.getElementById('adminform').getElementsByTagName('input');
        //now we parse them
        var elm=null;
        var total=0;
        for(cptCnt=0;cptCnt<aryCheck.length;cptCnt++) {
            elm=aryCheck[cptCnt];
            if(elm.type=='checkbox') {
                //we have a checkbox here
                if(elm.checked==true){
                    total++;
                }
            }
        }
        if(total > 0) {
            if(confirm("Are you sure you want to delete the selected records?")) {
                // SUBMIT THE FORM
                document.adminform.submit();
            }
        }
        else {
            alert("You didn't select any records");
        }
        break;


    case "edit":
        //we get an array with every input contained into the form, and the form have an id
        var aryCheck=document.getElementById('adminform').getElementsByTagName('input');
        //now we parse them
        var elm=null;
        var total=0;
        for(cptCnt=0;cptCnt<aryCheck.length;cptCnt++) {
            elm=aryCheck[cptCnt];
            if(elm.type=='checkbox') {
                //we have a checkbox here
                if(elm.checked==true){
                    total++;
                }
            }
        }
        if(total > 1) {
            alert("You can only edit one record at a time");
        }
        else if(total == 0) {
            alert("You didn't select a record");
        }
        else {
            document.adminform.submit();
        }
        break;

    default:
}

}

在你的表单中你需要像这样的东西

<form id="adminform" name="adminform" action="<?php $_SERVER['REQUEST_URI'] ?>" method="post">
<img src="/admin/images/news.png" alt="news" title="news" />
<input type="button" class="back" id="backbutton" title="go back" onclick="performAction('back');" />
<input type="button" class="delete" id="deletebutton" title="delete" onclick="performAction('delete');" />
<input type="button" class="archive" id="archivebutton" title="archive" onclick="performAction('archive');" />
<input type="button" class="edit" id="editbutton" title="edit" onclick="performAction('edit');" />
<input type="button" class="add" id="addbutton" title="add" onclick="performAction('add');" />
<table id="admintable"> 
    <tr><th class='tdleft'>
        <?php
        if($err !=0) {
            echo"<input type='checkbox' name='all' onclick='checkAll(adminform);' />";
        }   
        echo "</th><th class='tdright'>Title</th></tr>";
        $z = 0;
        // Iterate through the results
        while ($row = $result->fetch()) {
            if($z % 2==0) { 
                //this means if there is a remainder
                echo "<tr class='yellow'>'n";
                $z++;
            } else { 
                //if there isn't a remainder we will do the else
                echo "<tr class='white'>'n";
                $z++;
            }
            echo "<td class='tdleft'><input type='checkbox' name='id[]' value='{$row['id']}' /></td><td class='tdright'><a href='/admin/news/edit-news-".$row['id']."'>{$row['title']}</a></td></tr>";  
        }
    ?>  
</table>
<input type="hidden" id="action" name="action"  value="" />

和在HTML放置之前的页面顶部if ($ _POST,,array_key_exists("行动",$ _POST)) {

        // CARRY OUT RELAVANT ACTION
        switch($_POST['action']) {
            case "edit":
                foreach($_POST['id'] as $value) {
                    $id = $value;
                }   
                header('Location: /admin/blogs/edit-blog-'.$id);
                break;
            case "delete":
                if(!empty($_POST['id'])) {
                    //do your delete here
                }
                break;
            }
        }
            }