2010-12-15 45 views
0

我一直在尋找一個星期的答案,但我認爲我的理解不夠高,無法追蹤我發現的大部分內容。jquery,Ajax,PHP:編輯mysql條目

我基於教程使用了一個簡單的「將客戶端添加到數據庫」功能,現在我已經對其進行了修改,以便通過「編輯客戶端」按鈕從數據庫檢索記錄。 「編輯客戶」顯示了記錄列表

$('button.edit').click(function() { 
    //alert('this is an alert message'); 
    $('form#submit').hide(function(){$('div.showRecords').fadeIn()});  
}); 

(我知道這是草率的,我更願意把它填充表單時,「客戶編輯」按鈕被按下,但我不太有但在我的理解)

我有列表記錄工作,但在「記錄列表」中的每個記錄旁邊是編輯按鈕(每個記錄一個)。從這裏(不工作)我希望能夠$('button.editRecords')。click(function(){能夠從表單中編輯單個記錄...我感覺有點過頭了。 ..

在PHP函數中,ID在URL中傳遞到基於ID調用記錄的表單頁面我猜我需要做一些類似的事情,在我檢索表單的地方,這是我開始追我的尾巴...任何想法?

<script type"text/javascript"> 
$(document).ready(function(){ 

$('button.edit').click(function() { 
    //alert('this is an alert message'); 
    $('form#submit').hide(function(){$('div.showRecords').fadeIn()});  
}); 

$('button.editRecords').click(function() { 
    //alert('this is an alert message'); 
    $('form#submit').hide(function(){$('div.updateRecords').fadeIn()});  
}); 



$("form#submit").submit(function() { 
// we want to store the values from the form input box, then send via ajax below 
var fname  = $('#fname').attr('value'); 
var lname  = $('#lname').attr('value'); 
    $.ajax({ 
    url: "ajax.php", 
    type: "POST", 
    data: "fname="+ fname +"& lname="+ lname, 
    success: function(data) 
    {      
    if(data.success == 1) 
    { 
     alert ('success'); 
     //$('form#submit').hide(function(){$('div.success').fadeIn()}); 
    } 
    else 
    { 
     alert ('failure'); 
     //$('form#submit').hide(function(){$('div.failed').fadeIn()}); 
    }  
    } 
    }); 
return false; 
}); 

$("form#form1").submit(function() { 
// we want to store the values from the form input box, then send via ajax below 
var fname  = $('#fname').attr('value'); 
var lname  = $('#lname').attr('value'); 
    $.ajax({ 
    url: "update_ac.php", 
    type: "GET", 
    data: "fname="+ fname +"& lname="+ lname +"& id="+ id, 
    success: function(data) 
    {      
    if(data.success == 1) 
    { 
     alert ('success'); 
     //$('form#submit').hide(function(){$('div.success').fadeIn()}); 
    } 
    else 
    { 
     alert ('failure'); 
     //$('form#submit').hide(function(){$('div.failed').fadeIn()}); 
    }  
    } 
    }); 
return false; 
}); 

}); 
</script> 


<body> 
<br><br><br><br><br><br><br><br> 

<form id="submit" method="post"> 
    <fieldset> 
     <legend>Enter Information</legend> 

        <label for="fname">Client First Name:</label> 
     <input id="fname" class="text" name="fname" size="20" type="text"> 

        <label for="lname">Client Last Name:</label> 
     <input id="lname" class="text" name="lname" size="20" type="text"> 

     <button class="button positive"> <img src="../images/icons/tick.png" alt=""> Add Client </button> 
    </fieldset> 
</form> 

<div class="listRecords"> 
<button class="edit"> <img src="../images/icons/tick.png" alt="">Edit Clients</button> 
</div> 

<div class="showRecords" style="display:none;"> 
    <?php $sql="SELECT * FROM clients"; 
    $result=mysql_query($sql); 
?> 
    <table width="400" border="0" cellspacing="1" cellpadding="0"> 
     <tr> 
      <td> 
       <table width="400" border="1" cellspacing="0" cellpadding="3"> 
        <tr> 
        <td colspan="4"><strong>List data from mysql </strong> </td> 
        </tr> 

        <tr> 
        <td align="center" style="background-color:#FFF;"><strong>Name</strong></td> 
        <td align="center" style="background-color:#FFF;"><strong>Lastname</strong></td> 
        <td align="center" style="background-color:#FFF;"><strong>Edit</strong></td> 
        </tr> 
       <?php while($rows=mysql_fetch_array($result)){ ?> 
        <tr> 
        <td><? echo $rows['fname']; ?></td> 
        <td><? echo $rows['lname']; ?></td> 
        <td align="center"> 
         <!--- <a href="updated2.php?id=<? //echo $rows['id']; ?>">---><!--- This is where the form carried the id to updated2.php that I need to replicate in my jquery function ---> 
         <button class="editRecords"><img src="../images/icons/edit.gif" height="20" width="58" alt="Edit Record"><? echo $rows['id']; ?></button> 
          </a> 
         </td> 
        </tr> 
       <?php } ?> 
       </table> 
      </td> 
     </tr> 
</table> 
</div> 
<div class="updateRecord" style="display:none;"> 

<?php 
$id=$_GET['id']; 

// Retrieve data from database 
$sql="SELECT * FROM clients WHERE id='$id'"; 
$result=mysql_query($sql); 

$rows=mysql_fetch_array($result); 
?> 
<table width="400" border="0" cellspacing="1" cellpadding="0"> 
    <tr> 
     <form name="form1" method="post"> 
      <td> 
       <table width="100%" border="0" cellspacing="1" cellpadding="0"> 
        <tr> 
        <td>&nbsp;</td> 
        <td colspan="3"><strong>Update Records</strong> </td> 
        </tr> 
        <tr> 
         <td align="center">&nbsp;</td> 
         <td align="center">&nbsp;</td> 
         <td align="center">&nbsp;</td> 
         <td align="center">&nbsp;</td> 
        </tr> 
        <tr> 
         <td align="center">&nbsp;</td> 
         <td align="center"><strong>Name</strong></td> 
         <td align="center"><strong>Lastname</strong></td> 
        </tr> 
        <tr> 
         <td>&nbsp;</td> 
         <td align="center"><input name="fname" type="text" id="fname" value="<? echo $rows['fname']; ?>"></td> 
         <td align="center"><input name="lname" type="text" id="lname" value="<? echo $rows['lname']; ?>" size="15"></td> 
        </tr> 
        <tr> 
         <td>&nbsp;</td> 
         <td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td> 
         <td align="center"><input type="submit" name="Update" value="Update"></td> 
         <td>&nbsp;</td> 
        </tr> 
       </table> 
      </td> 
     </form> 
    </tr> 
</table> 
</div> 

我只是偶然的東西,可能會有幫助。如果我只是被編輯我原來的Ajax處理的文件,可以添加,查看和編輯項目,而不是用不同的ajax處理文件做這件事?


編輯12/20

我有現在的一切工作,一旦我的工作我會後對他人的結果,這些錯誤信息。在最後一塊被卡住的地方,一旦最終的編輯被提交回數據庫,錯誤對話總是錯誤地說它失敗了,但是信息在數據庫中得到更新。這裏是發生錯誤的最終ajax的代碼。

<?php 
include_once '../includes/opendb.php'; 
    include_once 'header.php'; 
    ?> 
<script type"text/javascript"> 
$(document).ready(function(){ 

$('button.buttonpositive2').click(function() { 
    //$("form#submit2").click(function() { 
     // we want to store the values from the form input box, then send via ajax below 
     var fname2  = $('#fname2').attr('value'); 
     var lname2  = $('#lname2').attr('value'); 
     var id  = $('#id').attr('value'); 
      $.ajax({ 
       url: "ajax4.php", 
       type: "POST", 
       //data: "fname="+ fname +"& lname="+ lname, 
       data: "id="+ id +"&fname="+ fname2 +"&lname="+ lname2, 
       success: function(data) 
        {      
         if(data.success == 'y') 
         { 
          alert ('success'); 
          //$('form#submit').hide(function(){$('div.success').fadeIn()}); 
         } 
         else 
         { 
          alert ('failure'); 
          //$('form#submit').hide(function(){$('div.failed').fadeIn()}); 
         }  
        } 
      });  
     return false; 
     }); 

/*$('button.positive2').click(function() { 
       var fname  = $(this).attr('value'); 
       var lname  = $(this).attr('value'); 
       var id  = $(this).attr('value'); 

       //alert('this is the edit button', 'Alert Dialog'); 
        $('div.updateRecord').fadeOut(function(){$('div.saveRecord').load("ajax4.php?id="+ id +"& fname="+ fname +"& lname="+ lname).fadeIn()});*/ 


}); 
</script> 
<?php 

echo '</head> 
     <body>'; 

    $id=$_GET['id']; 
    $sql="SELECT * FROM clients WHERE id='$id'"; 
    $result=mysql_query($sql); 
    $rows=mysql_fetch_array($result); 



    echo 
'<form id="submit2" method="post"> 
    <fieldset> 
     <legend>Edit Information</legend> 

        <label for="fname2">Client First Name:</label> 
     <input id="fname2" class="fname2" name="fname2" size="20" type="text" value="' . $rows['fname'] . '"> 

        <label for="lname2">Client Last Name:</label> 
     <input id="lname2" class="lname2" name="lname2" size="20" type="text" value="' . $rows['lname'] . '"> 

     <input id="id" name="id" type="text" class="text" value="' . $rows['id'] . '"> 

     <button class="buttonpositive2"> <img src="../images/icons/tick.png" alt=""> Save Edit </button> 
    </fieldset> 
</form>'    


       ; 

?> 

回答

1

我推薦使用加載函數來顯示要編輯的記錄。

將記錄的id值放在按鈕的ID屬性中,確保你的showRecords div也是空的。

$('button.edit').click(function() { 
    // Retrieves the id of the record you want to edit 
    var id = $(this).attr("id"); 
    $('form#submit').hide(function(){$('div.showRecords').load("record.php?id="+id).fadeIn()}); 
}); 

好運。

+0

工作甜! – Fred 2010-12-20 20:27:18

0

當您單擊添加/編輯按鈕,你應該叫一個Ajax功能,將通過所有數據添加或編輯,那麼你可以通過一個操作沿太(添加/編輯)。然後你的PHP腳本會知道對數據運行UPDATE或INSERT命令。

祝你好運!

+1

好吧,看起來我有一些學習要做。 – Fred 2010-12-15 20:32:05

+0

是的。但看起來你在查看結果方面做得很好。您將以同樣的方式編輯和更新ajax調用,但PHP腳本將有所不同。 – CrowderSoup 2010-12-15 21:56:18