2014-01-12 3 views
-1

새로 고치지 않고 DataBase로 작성한 후 html 테이블의 두 줄을 업데이트하려고합니다. 이제는 모든 것이 잘 작동합니다.이 경우 테이블에서 수정 된 값을 보려면 페이지를 새로 고쳐야합니다.이미 표시된 변수를 변경하는 방법은 무엇입니까?

<?php 
require_once('config.php'); 
$id=$_GET['id']; 
$practice=$_GET['practice']; 
$theory=$_GET['theory']; 
$sql="UPDATE `student_details` SET `ore_practice`='$practice', `ore_theory`='$theory' WHERE ID_student='$id'"; 
$result = mysql_query($sql); 
?> 

가 사전에 감사

first.php

<?php 
require_once('config.php'); 
$id=$_GET['id']; 
$sql="SELECT S.ID_student, C.name, D.h-theory, D.h-practice FROM students as S, students_details as D WHERE D.ID_student=C.ID_student AND S.ID_student=$id"; 
$result = mysql_query($sql); 
$row = mysql_fetch_array($result); 
echo'</br></br> 

<table class="my-tabel"> 
    <tr><td>Student name</td><td>'.$row['name'].'</td></tr> 
    <tr><td>Hours of practice/td><td>'.$row['h-theory'].'</td></tr> 
    <tr><td>Hours of theory</td><td>'.$row['h-practice'].'</td></tr> 
</table>'; 

?> 

<form action="javascript:void(0);" method="post"> 
    <label>Hours of practice:&nbsp&nbsp&nbsp </label> </td> <td><input type="text" id="practice" class="text" name="h_practice"/> 
    <label>Hours of theory:</label> </td> <td> <input type="text" id="theory" class="text" name="h_theory"/> 
    <input type="submit" class="send-h" value="Submit"> 
</form> 

<script> 
    $('.send-h').click(function(){ 
     $('.send-h').fadeOut(); 
     var practice = false; 
     var theory = false; 
     practice = $('#practice').val(); 
     theory = $('#theory').val(); 
     var id = <?php echo json_encode($id) ?>; 
     jQuery.get('add.php?practice='+practice+'&theory='+theory+'&id='+id, function(data,self){ 
     }); 
    }); 

</script> 

은 add.php : 여기

내 페이지입니다!

+0

AJAX를 사용하여 전송되는 데이터를 게시하려면 데이터베이스를 사용하십시오. – iBrazilian2

+0

음 ...분명히 몇 사람들은 대답을 게시 한 이후로 당신이 의미하는 것을 해독 할 수 있다고 믿지만, "이 모든 것이 제대로 작동하지 않으면 이제는 테이블에서 수정 된 값을보기 위해 페이지를 새로 고쳐야합니다. "당신이 의미하는 바를 알아내는 것은 말할 것도 없습니다. 질문을 더 명확하게 다시 말하면 다시 열기 위해 지명 할 것입니다. –

답변

1

먼저 사용할 수 있습니다

$ 아이디 = $ _GET [ 'ID를']; 는 mysql을 "= 적어도

$ 쿼리를 할 수 있도록 $ ID가 숫자 인 위험을 문자열에 대한

$query =" ... HERE D.ID_student=C.ID_student AND S.ID_student=$id"; 

사용 mysql_real_escape_string를 주입 // ... 여기 D.ID_student = C.ID_student 및 S.ID_student = ". ($ id + 0);

html 줄을 추가하려면 양식 데이터를받는 함수에서 ajax를 사용해야합니다. 쿼리가 여러 가지 이유로 실패 할 수 있음을 염두에

$('.send-h').click(function(){ 
    $.ajax({ 
    url: 'add.php?practice='+practice+'&theory='+theory+'&id='+id, 
    }).done(function(html_response) { 
    $("#YOUR_LIST_ID").append(html_response); 
    }); 
} 

곰 : http://api.jquery.com/jquery.ajax/

하나 그리 우아한 해결책은은 add.php 출력 표에 필요한 새로운 HTML을하고자 할 것이다. 이렇게하면 제출 한 데이터를 맹목적으로 추가해서는 안됩니다. add.php도 "success"플래그를 출력해야합니다.

지금은 add.php 캔 출력

echo json_encode(array('success'=>1, 'html'=> $generated_html)); //if all ok 

또는

echo json_encode(array('success'=>0, 'html'=> '')); //if all not ok 

과되는 .done (기능 (응답)) 당신이

var obj = jQUery.parseJSON(response); 
if (obj.success == 1) { 
    $("#YOUR_LIST_ID").append(obj.html); 
}else{ 
    alert('dberror'); 
} 

을에

또한 당신이 할 수 있습니다 이런 종류의 물건을 자주 사용할 계획이라면 Datatables ( http://datatables.net/examples/api/add_row.html) 을 사용하십시오.

0

난 당신이 echo $result; 같은 것이 될 수 있도록 add.php 파일에 응답 add.php에서 마지막 줄을 다음

<script> 
    $('.send-h').click(function(){ 
     $('.send-h').fadeOut(); 
     var practice = false; 
     var theory = false; 
     practice = $('#practice').val(); 
     theory = $('#theory').val(); 
     var id = <?php echo json_encode($id) ?>; 
     jQuery.get('add.php?practice='+practice+'&theory='+theory+'&id='+id, function(data,self){ 

      console.log(data); // Shows the returned data in the console 
      $('#my_id').html(data); // Shows the returned data in an element with the id `my_id` 

     }); 
    }); 

</script> 

할 수 있습니다 echo을 할 수있을 것으로 판단하고는 data 변수에 있어야합니다 귀하의 기능 중.

그리고 당신은 어떤 선택에 반환 된 콘텐츠를 표시 할 $(some_selector).html(data)를 사용하거나 당신은 또한 append, 모든 $(some_selector).append(data)

관련 문제