채택완료

name을 어떻게 써야하나요?

2018-04-04 (수) 18:46:37 1,885

단순 합계 내는 소스입니다.

디비에 저장이 안되서요.

input name 명이 

buy1, buy2... 이런식으로 들어가줘야 할것 같은데 그렇게쓰면 

스크립트가 작동안하고...

스크립트나 input의 name을 어떻게 써줘야 할련지요?^^

Copy
<div class="tbl_frm018 tbl_wrap estimate1">
    <table>
        <tr>
            <th rowspan="4" scope="row"><label for="wr_5">견적<strong class="sound_only"></strong></label></th>
            <td>


<form name="form">
<?
$wr50 = explode("|",$write['wr_50']);
$buy1 = $wr50[0];
$buy2 = $wr50[1]; 
$buy3 = $wr50[2]; 
$buy4 = $wr50[3]; 
?>            
            <input name="chkbox" type="checkbox" value="10000000" onClick="itemSum(this.form);" <?php echo ($buy1 == "10000000") ? "selected" : "";?>>&nbsp;일반형
            </td>
          </tr>
          <tr>
            <td height="25">
            <input name="chkbox" type="checkbox" value="20000000" onClick="itemSum(this.form);" <?php echo ($buy2 == "20000000") ? "selected" : "";?>>&nbsp;고급형</td>
          </tr>
          <tr>
            <td height="25">
            <input name="chkbox" type="checkbox" value="30000000" onClick="itemSum(this.form);" <?php echo ($buy3 == "30000000") ? "selected" : "";?>>&nbsp;프리미엄형</td>
          </tr>
          <tr>
            <td height="25">&nbsp;합계:&nbsp;<input name="total_sum" type="text" size="20" value="<?=$buy4?>" readonly> 원</td>
          </tr>
</form> 
 

</table>
</div>

 

 

//스크립트

<script language="javascript">
function itemSum(frm)
{
   var sum = 0;
   var count = frm.chkbox.length;
   for(var i=0; i < count; i++ ){
       if( frm.chkbox[i].checked == true ){
        sum += parseInt(frm.chkbox[i].value);
       }
   }
   frm.total_sum.value = sum;
}
</script>
|

답변 3개 / 댓글 1개

채택된 답변
+20 포인트

form 을 전송해서 DB에 저장하신다면,

name 을 다르게 해야 그 변수 그대로 받아서 저장할수 있구요.

입력 화면의 계산은 id="" 를 설정하셔서 처리하세요.

소스까지 만들어 드려야 하나요??

답변에 대한 댓글 1개

2018-04-04 (수) 20:15:29
네 하나만 부탁드려요^
2018-04-04 (수) 23:14:48

참고하실분들 보시든지...

Copy
<div class="tbl_frm018 tbl_wrap estimate1">

<?
$wr50 = explode("|",$write['wr_50']);
$item1 = $wr50[0];
$item2 = $wr50[1]; 
$item3 = $wr50[2]; 
$total = $wr50[3]; 
?>            
    <table>
        <tr>
            <th rowspan="4" scope="row"><label for="wr_5">통 인테리어<strong class="sound_only"></strong></label></th>
            <td>
            <input type=checkbox name=item1 value='10000000' onClick="sum(this)" <? if ($item1 == '10000000') echo "checked";?>> 일반형
            </td>
          </tr>
          <tr>
            <td height="25">
            <input type=checkbox name=item2 value='20000000' onClick="sum(this)" <? if ($item2 == '20000000') echo "checked";?>> 고급형</td>
          </tr>
          <tr>
            <td height="25">
            <input type=checkbox name=item3 value='30000000' onClick="sum(this)" <? if ($item3 == '30000000') echo "checked";?>> 프리미엄형</td>
          </tr>
          <tr>
            <td height="25">&nbsp;합계:&nbsp;<input type=text name=total value="<?=$total?>" readonly> 원 (vat 포함가)</td>
          </tr>
</table>

<script>
function sum(obj) {
  var oColl = obj.form.elements;
  var total = 0;
  for (var i=0; i < oColl.length; i++) {
    if (oColl[i].name.substr(0,4)=="item" && oColl[i].checked)
      total += parseInt(oColl[i].value);
  }
  obj.form.total.value = total;
}
</script>

</div>
2018-04-04 (수) 23:09:01

eyekiss 님 조언대로

id값으로 계산하니까 쉽게 해결되네요 감사합니다.

답변을 작성하려면 로그인이 필요합니다.