2013-08-26 2 views
0

쿼리 및 아약스에 의해 테이블의 행을 업데이트하려고합니다.페이지 코드화기를 새로 고치지 않고 db를 아약스로 업데이트하십시오.

성공했지만 문제가 하나 있습니다.

내가 가진 페이지는 하나의 입력이있는 양식입니다. 입력은 DB의 행에 대해 id입니다. 양식을 제출 한 후 행을 업데이트하지만 입력에 다른 id을 삽입하면 페이지를 다시 새로 고칠 때까지 작동하지 않습니다. 의미 : 테이블의 다른 행을 업데이트하려면 페이지를 다시 새로 고쳐야합니다. 나는 codeigniter를 사용하고있다.

내 코드입니다 :

<form method="POST" action="" > 
    <fieldset data-role="controlgroup"> 
     <input type="text" name="id" id="id" value="" 
     <input type="submit" name="submit-choice-a1" id="submit-choice-b1" value="submit" /> 
    </fieldset> 
</form> 

와 JS는 다음과 같습니다

1) <form> 태그를 사용하지 말라, 단지 정보를 얻을 보내

<script type="text/javascript"> 
$(function() { 

    $("#submit-choice-b1").click(function(e) { 

     var id = $('#id').val(); 
     var result = confirm("are you sure ?"); 
     if (result==true) { 

      $.ajax({ 
       url: "the url that have the page with the update function", 
       success: function(xhr){ 
        alert('updated'); 
       }, 
       error: function (xhr, ajaxOptions, thrownError) { 
        alert(xhr.status); 
        alert(xhr.responseText); 
       } 
      }); 
     } 
    }); 
}); 
</script> 

답변

1

나는이 방법을 참조 그것은 데이터 매개 변수에서 jQuery 아약스와 함께.

$.ajax({ 
url: "the url", 
type: "post", 
data: { 
    id: 68,//Your field id/class, got it with $(selector).attr("id") or $(selector).attr("class"); 
    anotherKey: anotherValue 
    etc... 
} 
}); 

2) onsubmit이

<form method="post" onsubmit="return sent();"> 

전송 된() 함수의 끝을 속성을 사용하여 양식을 수정할 때 (페이지를 다시로드 해달라고) 추가 폼에 말해 "false를 돌려"필요

function sent() { 
    ..your ajax implementation here.. 
    return false; 
} 
+1

() '컨트롤러', 배열 ('onsubmit'=> '당신의 js')이 대답은 정확하지만 CI의 형태 form_open 컨트롤러, 내장을 사용하는 것이 더 바람직 할 것이다; –

관련 문제