2016-07-02 13 views
0

whileループ中にボタンをクリックすると、特定の結果のみをajaxで表示します。js、mysql、php - whileループを介してajaxリクエストの特定の結果を取得

while($row = mysql_fetch_array($rs_admin)) 
{ 

echo ("<h3>" .$row['emer_type'] . "</h3> "); 
echo ("<p>".$row['emer_desc'] . "</p>"); 
echo '<div class="button"><button type="button" onclick="loadDoc()">Change Content</button></div>': 
echo '<div id="demo"><p>I WANT TO PUT IT HERE</p></div>'; 
} 

これらのIDは独自のIDを持ち、取得する内容を知っています。 これは私が

<script> 
function loadDoc() { 
    var xhttp = new XMLHttpRequest(); 
    xhttp.onreadystatechange = function() { 
    if (xhttp.readyState == 4 && xhttp.status == 200) { 
     document.getElementById("demo").innerHTML = xhttp.responseText; 
    } 
    }; 
    xhttp.open("GET", "test.php", true); 
    xhttp.send(); 
} 
</script> 

を持ってtest.phpをこの

<?php 
include 'includes/db_connection.php'; 
$sel_admin = "select * from ehr_cm_emergency "; 
$rs_admin = mysql_query($sel_admin); 
while ($row = mysql_fetch_array($rs_admin)) 
{ 
    echo $row['emer_more']; 

    } 
?> 

である私は2番目か3番目のボタンのボタンをクリックしていしかし、結果はで表示しているアヤックスであります最初の1つ。

答えて

1

私は独特のためにEMP_IDを使用 をベース自動インクリメントによってidを区別し、関数に渡すと、次のようなIDにテキストに適用され、特定の行の

<?php 

    $i=0; 
    while($row = mysql_fetch_array($rs_admin)) 
    { 
     $i++; 
     echo ("<h3>" .$row['emer_type'] . "</h3> "); 
     echo ("<p>".$row['emer_desc'] . "</p>"); 
     echo '<div class="button"><button type="button" onclick="loadDoc(\'demo'.$i.'\')">Change Content</button></div>'; 
     echo '<div id="demo'.$i.'"><p>I WANT TO PUT IT HERE</p></div>'; 
    } 

?> 

    <script> 
    function loadDoc(id) { 
     var xhttp = new XMLHttpRequest(); 
     xhttp.onreadystatechange = function() { 
     if (xhttp.readyState == 4 && xhttp.status == 200) { 
      document.getElementById(id).innerHTML = xhttp.responseText; 
     } 
     }; 
     xhttp.open("GET", "test.php", true); 
     xhttp.send(); 
    } 
    </script> 

、あなたの値を変更

<?php 

    $i=0; 
    while($row = mysql_fetch_array($rs_admin)) 
    { 
     $i++; 
     echo ("<h3>" .$row['emer_type'] . "</h3> "); 
     echo ("<p>".$row['emer_desc'] . "</p>"); 
     echo '<div class="button"><button type="button" onclick="loadDoc(\'demo'.$i.'\','.$row['emp_id'].')">Change Content</button></div>'; 
     echo '<div id="demo'.$i.'"><p>I WANT TO PUT IT HERE</p></div>'; 
    } 

?> 

    <script> 
    function loadDoc(id,empid) { 
     var xhttp = new XMLHttpRequest(); 
     xhttp.onreadystatechange = function() { 
     if (xhttp.readyState == 4 && xhttp.status == 200) { 
      document.getElementById(id).innerHTML = xhttp.responseText; 
     } 
     }; 
     xhttp.open("GET", "test.php?empid="+empid, true); 
     xhttp.send(); 
    } 
    </script> 


<?php 
include 'includes/db_connection.php'; 
$sel_admin = "select * from ehr_cm_emergency where emp_id=".$_GET['emp_id']; 
$rs_admin = mysql_query($sel_admin); 
while ($row = mysql_fetch_array($rs_admin)) 
{ 
    echo $row['emer_more']; 

    } 
?> 
+0

'エコー '

' を使用して、同意する;'まだ大丈夫 –

+0

Nice、ありがとう... – Mani

+0

whileループにtest.phpに変数を渡す方法を使用します。それはエコーを表示しません。各行の表示は現在動作していますが! – Noobster

関連する問題