2014-01-19 7 views
0

사이트에 사용되는 다른 PHP 테이블에 SQL 데이터를 추출 중입니다. 표의 각 행마다 해당 행을 삭제하거나 편집 할 수있는 버튼이 있습니다. 편집 버튼은 정상적으로 작동하지만 삭제 버튼에 문제가 있습니다. 현재 삭제 버튼은 삭제를 위해 선택된 행이 아닌 테이블의 첫 번째 행만 삭제합니다. 여기 여러 형태로 반복하기

테이블의 샘플입니다

이 데이터는 아약스 호출에 전송되는
while($row = mysqli_fetch_array($result)){ 
    <tr> 
    <td>'.($row['some_data']).'</td> 
    <td> 
     <form action="meeting_update_milestone.php" method="POST"> 
     <input type="hidden" name="mile_id" value="'.$row['id'].'" /> 
     <input type="submit" value="EDIT" /> 
     </form> 
    </td> 
    <td> 
     <form id="meeting_delete_item"> 
     <input type="hidden" name="mile_id" value="'.$row['id'].'" /> 
     <input type="button" onclick="delete_meeting_item()" value="DELETE" /> 
     </form> 
    </td> 
    </tr>'; 
} 

: 당신은 여러 양식을 만드는 행을 통해 I 루프를 말할 수

function delete_meeting_item(){ 
    var x = confirm("Are you sure you want to permanetly delete this item?"); 
    if (x){ 
    $.ajax({ 
     type: "POST", 
     url: "meeting_delete_item.php", 
     data: $('#meeting_delete_item').serialize(), 
     cache: false, 
     success: function(result){ 
     alert(result); 
     } 
    })//end ajax 
    } 
    else{ 
    return false; 
    } 
} 

같은 이드. 나는 이것이 여기에 문제가되고 있다고 생각하니? 그렇다면 동일한 서식을 유지하여 어떻게 해결할 수 있습니까? 양식에 고유 한 이름이 있어야한다고 생각합니다. 그러나 이것을 아약스 데이터 문자열로 구현하는 방법을 모르겠습니다.

답변

0

나는 당신의 PHP 코드의 유효성에 대해 언급 할 수 없지만, 당신이 말하는대로 id 값이 고유하게 이 경우 ID가 실제로 달성하려는 목표와 크게 다르지 않다고 저는 생각합니다.

while($row = mysqli_fetch_array($result)){ 
    <tr> 
    <td>'.($row['some_data']).'</td> 
    <td> 
     <form action="meeting_update_milestone.php" method="POST"> 
     <input type="hidden" name="mile_id" value="'.$row['id'].'" /> 
     <input type="submit" value="EDIT" /> 
     </form> 
    </td> 
    <td> 
     <form> 
     <input type="hidden" name="mile_id" value="'.$row['id'].'" /> 
     <input type="button" class="delete" value="DELETE" /> 
     </form> 
    </td> 
    </tr>'; 
} 

을 그리고 jQuery를 사용하여 바인딩 :

id 및 인라인 onclick 이벤트를 제거합니다. 삭제 버튼에 셀렉터를 추가 할 수있는 클래스를 추가했습니다.

$(document).ready(function(){ 
    $('.delete').click(function(){ 

     var x = confirm("Are you sure you want to permanetly delete this item?"); 
     if (x){ 
     $.ajax({ 
      type: "POST", 
      url: "meeting_delete_item.php", 
      data: $(this).closest(form).serialize(), 
      cache: false, 
      success: function(result){ 
      alert(result); 
      } 
     })//end ajax 
     } 
     else{ 
     return false; 
     } 
    } 
} 

삭제 버튼을 클릭하면 가장 가까운 양식이 표시되고 해당 양식의 데이터가 일련 번호로 표시됩니다.

0

루프 밖에서 카운터를 유지할 수 있습니다 : $x = 0 및 양식을 생성 할 때 사용 : <form id="meeting_delete_item"'.$x.'>. 루프가 끝나면 다음 반복을 위해 카운터를 증가시킵니다 ++$x. 귀하의 자바 스크립트에서, 당신은 아마 눌러 버튼에 따라 ID를 검색합니다.

샘플 양식 :

<form id="myForm1"> 
    <button>Click me!</button> 
    </form> 
    <form id="myForm2"> 
    <button>Click me!</button> 
    </form> 

샘플 자바 스크립트 :

$('form button').click(function(e) { 
    var $form = $(this).closest('form'); 
    console.log($form.attr('id')); 
    return false; //stop form submission 
}); 

또한 Live demo (click).

, 인라인 JS는 (당신의 HTML에서 온 클릭과 같은) 좋은 적이있다 연습. 대신 샘플 코드에서 예제를 따르고 javascript로 click 함수를 바인딩하십시오. 이 주제에 대한 자세한 내용은 이러한 결과의 일부를 읽어 https://www.google.com/search?q=Why+is+inline+js+bad%3F

0

귀하의 문제가 여기

$('#meeting_delete_item').serialize() 
당신은 현재 행이 있는지 결정하기 위해 다른 것을 사용한다

입니다. DELETE

<form id="meeting_delete_item"> 
    <input type="button" onclick="delete_meeting_item('.$row['id'].')" value="DELETE" /> 
    </form> 

에 대한

HTML의 FORM 부분 JS

function delete_meeting_item(deleteId){ 
    var x = confirm("Are you sure you want to permanetly delete this item?"); 
    if (x){ 
    $.ajax({ 
     type: "POST", 
     url: "meeting_delete_item.php", 
     data: {mile_id: deleteId}, 
     cache: false, 
     success: function(result){ 
     alert(result); 
     } 
    })//end ajax 
    } 
    else{ 
    return false; 
    } 
}