具有多行和删除的表

时间:2011-11-21 20:45:37

标签: php javascript mysql html

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

我试图找出添加删除和编辑功能的最佳方法。我正在考虑编辑,我将不得不使用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>

目前我有del和编辑设置为按钮,仅供可见参考。处理有这样多个按钮的情况的最佳方法是什么?

4 个答案:

答案 0 :(得分:1)

如果我的答案过于广泛,我会道歉,但你的问题也是如此。

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

以下是关于如何使用JQuery执行ajax请求的简介:

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

答案 1 :(得分:0)

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

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

按钮的值将是行的id,名称将是按钮将执行的操作。我要做的下一件事是将这些按钮绑定到jquery的动作。

$('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
});

答案 2 :(得分:0)

删除行

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 并执行相同的操作,从数据库中输入值,然后更新它

答案 3 :(得分:0)

这是你的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&amp;&amp; array_key_exists(“action”,$ _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;

            }
        }
            }