2014-09-29 3 views
0

테이블 행에서 onclick 이벤트 쿼리를 실행하려고합니다.MySQL 테이블 행 onclick 이벤트

자바 스크립트에서 함수를 만들었으므로 각 행의 ID를 읽을 수 있습니다.

이제이 ID를 조건으로 쿼리를 실행할 수 있기를 원합니다.

나를 도와 줄 수 있습니까?

내 질문을 실현 하시겠습니까?

아래 코드를 보여 드리겠습니다.

------------- Javascript 기능 -----------------!

<script> 
function addRowHandlers() { 
    var table = document.getElementById("tableId"); 
    var rows = table.getElementsByTagName("td"); 
    for (i = 0; i < rows.length; i++) { 
     var currentRow = table.rows[i]; 
     var createClickHandler = 
      function(row) 
      { 
       return function() { 
             var cell = row.getElementsByTagName("td")[1]; 
             var id = cell.innerHTML; 
             alert("id:" + id); 
           }; 
      }; 

     currentRow.onclick = createClickHandler(currentRow); 
    } 
} 
window.onload = addRowHandlers(); 
</script> 

는 ----- 쿼리 나는 당신이 당신의 id을 보낼 Ajax를 사용할 필요가 -----------

$result= mysql_query("select * from tasks where idTask = id"); 

while($row = mysql_fetch_array($result)) 
{ 
    $image = $row['image']; 
    header("Content-type:image/jpeg"); 
    echo $image; 
} 

enter image description here

+0

ajax.php ("ID :"+ ID가)', 당신은 당신의 PHP 파일에 Ajax 호출을 할 필요가있다. 여기에는 표준 자바 스크립트와 jquery 라이브러리를 사용하는 많은 예제가 있습니다. – Sean

+0

답장을 보내 주셔서 감사합니다. PHP 프로그래밍에 익숙하지 않습니다. 좀 더 명쾌해질 수 있겠 니? – rpirez

+0

표준 자바 스크립트를 사용하는 Ajax - jQuery 라이브러리를 사용하는 Ajax - http://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery - http://stackoverflow.com/questions/5004233/jquery-ajax-post-example-with-php와 양쪽 모두 약간 - http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – Sean

답변

2

실행하려는 별도의 PHP 파일을 누른 다음 데이터를 반환합니다. 내가 jQuery 라이브러리를 사용하기 때문에이 예제는 $.post() 사용 - (http://api.jquery.com/jquery.post/)을하고 JQuery와 UI .dialog() - (http://jqueryui.com/dialog/가)

<script src="//code.jquery.com/jquery-1.10.2.js"></script> 
<script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css"> 
<script> 
function addRowHandlers() { 
    var table = document.getElementById("tableId"); 
    var rows = table.getElementsByTagName("td"); 
    for (i = 0; i < rows.length; i++) { 
     var currentRow = table.rows[i]; 
     var createClickHandler = 
      function(row) 
      { 
       return function() { 
             var cell = row.getElementsByTagName("td")[1]; 
             var id = cell.innerHTML; 

             //use ajax to post id to ajax.php file 
             $.post("ajax.php", { id:id }, function(data) { 
               //add result to a div and create a modal/dialog popup similar to alert() 
               $('<div />').html('data').dialog(); 
             }); 

           }; 
      }; 

     currentRow.onclick = createClickHandler(currentRow); 
    } 
} 
window.onload = addRowHandlers(); 
</script> 

ajax.php가 (별도의 파일을 무작위로 명명 된 자바 스크립트 코드 alert()

유사한 결과를 팝업하는 당신의`경고 대신에)

$id = mysql_real_escape_string($_POST['id');  

$result= mysql_query("select * from tasks where idTask = $id"); 

while($row = mysql_fetch_array($result)) 
{ 
    $image = $row['image']; 
    header("Content-type:image/jpeg"); 
    echo $image; 
} 
+0

답장을 보내 주셔서 대단히 감사합니다. 사과 할 필요없고, 가족이 먼저 온다. 당신이 넣은 코드를 사용했는데 결과는 여러 문자로 된 상자였습니다. 인쇄 화면을 게시하고 싶습니까? 어떻게하면 좋을까요? 진심으로 인사드립니다. – rpirez

+0

도와 주셔서 대단히 감사합니다. 선생님, 제 인생을 구하십시오 선생님 :) – rpirez