2011-11-21 60 views
0

好的,所以我有一個輸入函數,允許我將項添加到數據庫,並將其顯示爲表。作爲表的一部分,我試圖添加刪除和編輯按鈕。帶有多行和刪除的表

我想找出添加刪除和編輯功能的最佳方法。我正在考慮編輯,我將不得不使用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> 

目前我有德爾和編輯菜單按鈕,僅供參考可見。處理這種情況的最佳方法是什麼?

+0

你想AJAX編輯和刪除或只是在新的頁面PHP? – Sedz

+0

只是php,雖然我知道一些基本的AJAX,但在大多數情況下,這對我來說仍然太複雜。 –

+0

目前我只是phping新頁面,然後使用Javascript立即重定向回主頁面。也許不是最有效的方式,但容易。 –

回答

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 
}); 
1

我很抱歉,如果我的回答太寬泛,但你的問題也是如此。

編輯和刪除都應該使用JavaScript和PHP代碼的組合;例如,當用戶點擊刪除按鈕時,您可以向服務器發送Ajax請求,從數據庫中刪除該記錄,並在成功從服務器端調用返回時,使用JavaScript從標記中直觀地刪除記錄。這同樣適用於編輯功能。

下面是有關如何使用執行Ajax請求JQuery的一個很好的介紹:

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

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 ,做同樣的,你輸入把值從數據庫中,然後對其進行更新

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頁面之前把 如果($ _ 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; 

      } 
     } 
      }