2013-01-08 2 views
2

PHP를 통해 mySQL 데이터베이스에 해당 값을 제출하고 저장하려는 여러 양식의 페이지가 있습니다. 내 문제는 질문 (submitting multiple forms with AJAX) 이상적으로 처리하고 데이터베이스에 저장됩니다 onChange() 각 양식을 제출하고 싶습니다 제외하고 매우 비슷합니다. 이 같은 일반적인 모양의양식 제출 AJAX로 여러 양식의 OnChange 변경

내 양식 :

<form name="form1" id="form1" action="" method="POST"> 
    <input type="text" class="input-block-level" placeholder="Placeholder text"> 
    <input type="text" class="input-block-level" placeholder="Placeholder text"> 
</form> 
.... 
<form name="form2" id="form2" action="" method="POST"> 
    <input type="text" class="input-block-level" placeholder="Placeholder text"> 
    <input type="text" class="input-block-level" placeholder="Placeholder text"> 
</form> 
.... 

그리고 내 현재 jQuery를/AJAX 코드 : 이상적으로

$(document).ready(function() { 
    $('#form1').change(function() { 
    $.post('process.php', $("#form1").serialize(), function(data) { 
     $('#results').html(data); 
     }); 
    }); 

}); 

, 나는 모든 형태의이 조각을 복사/붙여 넣기 할 필요가 없습니다 것입니다 (나는 많은 다른 형식을 가지고있다.) 그러나 변경되었을 때 어떤 형태로든 작동하는 코드 블록을 가질 수있다. 어떤 도움이라도 대단히 감사 할 것입니다.

답변

1

모든 양식에 CSS 클래스를 추가하고 모두 동일한 작업을 수행하도록 검색 할 수 있습니다.

$(document).ready(function() { 

    // Retrieve all form on the page 
    $('.formAjax').change(function() { 
     $.post('process.php', $(this).serialize(), function(data) { 
      // Update the result area 
      $('#results').html(data); 
     }); 
    }); 
}); 

모든 양식에 우수한 CSS 클래스를 추가했다고 가정합니다.

<form name="form1" id="form1" action="" class="formAjax" method="POST"> 

참고 : 각 양식의 ID가

+0

아 쓸모가 그것에 대해 생각하지 않았다. 좋아, 고마워! – oIovoIo