2010-02-18 114 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>