2016-10-22 2 views
0

두 개의 텍스트 상자와 하나의 결과 열을 사용하여 동적으로 생성 된 PHP 루프가 있습니다.PHP 루프에서 값 가져 오기 및 키패드에서 여러 값 가져 오기

내가 필요한 것은 첫 번째 두 개의 상자에 입력 한 값을 여러 개 표시하면 가까운 결과 상자에 결과가 표시됩니다.

PHP의 루프

<?php 
$sql=$db->query("SELECT * FROM calc"); 
foreach($sql as $row) 
{ 
    ?> 
<div class="calc"> 
<input type="text" id="txt1id<?= $row['id'] ?>" onkeyup="multiple()"> 
<input type="text" id="txt2id<?= $row['id'] ?>" onkeyup="multiple()"> 
<input type="text" id="resultid<?= $row['id'] ?>" title="Result shows here automatically"> 
</div> 
<?php } ?> // loop ends here. 

스크립트이

<div class="calc"> 
<input type="text" id="txt1id<?= $row['id'] ?>" onkeyup="multiple(<?= $row['id'] ?>)"> 
<input type="text" id="txt2id<?= $row['id'] ?>" onkeyup="multiple(<?= $row['id'] ?>)"> 
<input type="text" id="resultid<?= $row['id'] ?>" title="Result shows here automatically"> 
</div> 

와 같은 JS에서의 변화와 같은 기능

<script> 
function multiple() 
{ 
    // how to get two different variables like this with different id to multiple ? 
    var firstBox = parseInt(document.getElementById(firstid).value); 
    var secondBox = parseInt(document.getElementById(secondid).value); 


} 
</script> 
+0

PHP 값이 필요한 곳은 어디입니까? 또는 PHP 루프는 어디에 있습니까? – C2486

+0

'div class = 'calc''가 php 루프에 의해 생성되었지만 php 테이블에 다른 값이 포함되어 있지만 여기에 포함되지 않았습니다. 여기서 ID는 데이터베이스에서 가져 오는 것입니다 .. 'txt1id1, txt2id1' 등 – teashark

+0

PHP 코드를 공유 할 수 있습니까? 또한? – C2486

답변

1

패스 행 ID이다

<script> 
function multiple(rowid) 
{ 
    var firstBox = parseInt(document.getElementById("txt1id"+rowid).value); 
    var secondBox = parseInt(document.getElementById("txt2id"+rowid).value); 
} 
</script> 
아래
+0

'var secondBox = parseInt (document.getElementById ("txt1id"+ rowid) .value);', txt1id는'txt2id' 여야합니다. – teashark

+0

네 변경 해주십시오 – C2486

+0

또한 console.log ("Row ID :"+ rowid);의 콘솔에서 ROWID를 확인하십시오. – C2486