2014-01-12 35 views
-1

我想在沒有刷新的情況下寫入DataBase後更新html表的兩行。現在一切正常,除非這件事,我必須刷新頁面才能看到表中的修改值。如何更改已顯示的變量?

這裏是我的網頁:

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

<?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); 
?> 

預先感謝您!

+0

使用AJAX發送正在發送的數據做數據庫。 – iBrazilian2

+0

呃...顯然有一些人認爲他們可以破譯你的意思,因爲他們發佈了答案,但我甚至不能解析「現在一切工作正常,除非這件事,我必須刷新頁面才能看到表中的修改值「更別說弄清楚你的意思了。請更清楚地重申這個問題,我會提名重新開放。 –

回答

1

首先:

的$ id = $ _GET [ '身份證']; // MySQL的注入危險字符串

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

使用mysql_real_escape_string $ id爲數字那麼至少做

$查詢=」 ...這裏D.ID_student = C.ID_student AND S.ID_student = 」($ ID + 0);

要添加html行,您需要在接收表單數據的函數中使用ajax。 http://api.jquery.com/jquery.ajax/

一個不那麼優雅的解決方案將是add.php將輸出表中所需要的新的HTML:

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

記住,查詢可能會失敗有多種原因。 這就是爲什麼你不應該盲目地添加你所提供的數據。 add.php還必須輸出「成功」標誌。

所以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'); 
} 

你也可能要使用數據表(http://datatables.net/examples/api/add_row.html) 如果你打算經常使用這種東西。

0

我相信你應該能夠做到以下幾點

<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> 

您可以echoadd.php與響應所以像echo $result;將是你的最後一行在add.php文件,應該是在data變量你的功能。

然後你就可以使用$(some_selector).html(data)來顯示一些選擇返回的內容,或者您​​也可以使用append$(some_selector).append(data)