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

그리고 index.php.txt에서

Jquery.editableText.js.txt

하면 데이터베이스에서 내 데이터를 검색하고 조금 사용하는 인덱스 페이지입니다 jQuery 코드. jQuery.editableText.js.txt에는 구체적인 jQuery 코드가있다. PHP 처리기 페이지는 올바른 필드를 가져 와서 데이터베이스에서 업데이트하는 것으로 거의 표준입니다.

답변

1

난 당신에 대한 질문이 :

  1. $ menuId와 뭔가의 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>