2010-02-18 103 views
0

我正在開發一個主頁,並將爲管理員使用AJAX內聯編輯腳本,以使其儘可能簡單。 我一直在使用的腳本是this,它具有幾乎所有我想要的內聯編輯腳本。當我要捕獲新的更改並將它們發送給PHP函數時,我的問題就會出現,它將使用這些新更改更新我的數據庫。AJAX內聯編輯:將PHP更新添加到新更改中

我沒有與AJAX和PHP太多的合作經驗,所以我多少有些失落,但我已經嘗試了代碼,我發現:

$.ajax({ 
    type: "POST", 
    url: "update_handler.php", 
    data: "newfieldvalue=" + newValue, 
    success: function(msg){ 
    alert("Data Saved: " + msg); 
    } 
}); 

的問題是,我不太知道如何或在哪裏實施這個代碼,或者如果它甚至是正確的代碼使用。爲了顯示你的代碼中,我掛的兩個TXT文檔:

Index.php.txt

而且

Jquery.editableText.js.txt

index.php.txt是索引頁面,它從數據庫中檢索我的數據,並使用一個位的jQuery代碼。在jQuery.editableText.js.txt是具體的jQuery代碼。我猜想PHP處理程序頁面在獲取正確的字段並將其更新到數據庫中時非常標準。

回答

1

我有問題要問你:

  1. $菜單ID中包含的東西的ID和你用它來通過這個ID從表中獲取它。這是正確的?

如果它是正確的,你必須通過這個ID的PHP處理程序頁面。

實施例:

的index.php:

<script type="text/javascript"> 
jQuery(function($){ 
    $('h2.editableText, p.editableText').editableText({ 
     newlinesEnabled: false 
    }); 

    $.editableText.defaults.newlinesEnabled = true; 

    $('div.editableText').editableText(); 

    $('.editableText').change(function(){ 
     var newValue = $(this).html(); 

     // important code: 
      $.ajax({ 
      type: "POST", 
      url: "save.php", 
      data: { val : newValue, key:$(this).parent().tagName, id:$(this).parent().attr('class')}, 
      success: function(msg){ 
      alert("Data Saved: " + msg); 
      } 
     }); 

    }); 

}); 
</script> 

和身體部分:

<body> 
<div id="wrapper"> 
<div id="content"> 
    <?php 
    $isnull = getContent($menuID, "title"); 
    if ($isnull != "") { 

    echo "<h2 class=\"editableText\"><center><p>" . getContent($menuID, "title") . "</p></center></h2><br>"; 
    } else { 
     null; 
    } 
    ?> 

    <div class="editableText"> 

<p class="<?php echo $menuID?>"><?php echo getContent($menuID, "maincontent");?></p> 
     </div> 
    </script> 
    <?php 

    mysql_close($connection); 
?> 

和一多,save.php:

<?php 

# content that you send from client; you must save to maincontent 
$content=$_POST['val']; 

# $from=='div' if it from maincontent or $from=='center' if it from title 
$from=$_POST['key']; 


# id of your post 
$id=$_POST['id']; 

    #here you can save your content; 
?> 
0

正如edit-in-page page所述,您應該在腳本塊中使用該代碼。所以你幾乎擁有它。以下應該工作(未經測試)。

<script type="text/javascript"> 
    jQuery(function($){ 
     $('h2.editableText, p.editableText').editableText({ 
      newlinesEnabled: false 
     }); 

     $.editableText.defaults.newlinesEnabled = true; 

     $('div.editableText').editableText(); 

     // bind an event listener that will be called when 
     // user saves changed content 
     $('.editableText').change(function(){ 
      var newValue = $(this).html(); 

      // do something 
      // For example, you could place an AJAX call here: 
      $.ajax({ 
       type: "POST", 
       url: "update_handler.php", 
       data: "newfieldvalue=" + newValue, 
       success: function(msg){ 
       alert("Data Saved: " + msg); 
       } 
      }); 
     }); 

    }); 
</script>