2011.07.13 17:48

JavaScript에서 폼 submit 처리하기

자바스크립트 부분
<script type="text/javascript">
function submitForm(action){
    document.record.innerHTML = "<input type=hidden name=submit value='" + action +"'>";
    document.change_record.submit();
}
</script>


호출하는 부분
<form name="change_record" action="record_update.php" method="post" autocomplete="off">

<a name="edit" href="javascript:void(1);" onClick="submitForm('Edit');">Edit</a>

<a name="delete" href="javascript:void(1);" onClick="submitForm('Delete');">Delete</a>

만약 클릭에 따라서 폼의 action이 달라져야 한다면 각 action 별로 폼을 작성하고 해당 폼의 submit을 호출

예)
자바스크립트
<script type="text/javascript">
function submitForm(action) {
 if(action == 'Edit') {
     document.form1.innerHTML = "<input type=hidden name=submit value='" + action +"'>";
     document.form1.submit();
 }
 else if(action == 'Delete') {
  document.form2.innerHTML = "<input type=hidden name=submit value='" + action +"'>";
     document.form2.submit();
 }
}
</script>

호출하는 부분
<body>
<input type="button" name="edit" onclick="submitForm('Edit')" value="수정">
<input type="button" name="delete" onclick="submitForm('Delete')" value="삭제">
<div>
 <form name="form1" method="post" action="edit.html">
 </form>
</div>
<div>
 <form name="form2" method="post" action="delete.html">
 </form>
</div>
</body>


[참고] using JavaScript for form submit
Trackback 0 Comment 1