我試圖做一個簡單的信用頁面,人們卸下信貸那麼多的信貸,並通過點擊一個按鈕,他們的名字旁邊會取消對信貸上按下一個按鈕PHP MySQL的AJAX
<table border="1">
<tr>
<td>Name</td><td>Massages left</td>
</tr>
<?php
mysql_connect("localhost","dbuser","password");
mysql_select_db("db");
$command = "select pass_name, credit_left from pass;";
$result = mysql_query($command);
//If there is, list that particular entry
while ($data = mysql_fetch_object($result))
{
print "<tr><td>" . $data->pass_name . "</td>" .
"<td>" . $data->credit_left . "</td></tr>\n"
// Button here that you click and make credit go down one;
}
?>
</table>
例如它會說Ben - 2credits - 點擊這裏刪除一個。
感謝
編輯
還好這裏是我有:
劇本
<script>
function removeOneCredit(str)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("credit").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","removeonecredit.php"+str,true);
xmlhttp.send();
}
</script>
形式是:
while ($data = mysql_fetch_object($result)) {
print "<TR><TD>".$data->pass_name."</TD><TD id='credit'>".$data->credit_left."</TD><TD><form><input type='submit' value='- 1' onsubmit='removeOneCredit(?pass_id=".$data->pass_id."&credit_left=".$data->credit_left.")'></form></TD></TR>\n";
}
的removeonecredit.php是
<html>
<?php
$pass_id = $_GET[pass_id];
$credit_left = $_GET[credit_left]-1;
//change value
mysql_connect("localhost","user","pw");
mysql_select_db("db");
$command = "update pass set credit_left='$credit_left' where pass_id='$pass_id';";
mysql_query($command);
//now print value
mysql_connect("localhost","user","pw");
mysql_select_db("db");
$command = "select credit_left from pass where pass_id='$pass_id';";
$result = mysql_query($command);
$data = mysql_fetch_object($result);
echo $data->credit_left;
?>
</html>
如果我手動傳遞字符串在removeonecredit.php年底,它的工作原理,所以它必須是對事物的阿賈克斯部分...
好了,什麼是你的問題?請明確點。 – hesson
那麼你會使用什麼樣的代碼?我應該創建一個發送sql查詢以使「credit_left減1」並重新加載的表單嗎?如果是的話,我該如何將該查詢應用於該用戶(同時遍歷我的用戶) –
我不明白爲什麼要以這種方式將pass_id和credit_left傳遞給AJAX函數。你不應該真的傳遞這樣的信息。你應該使用會話在你的removeonecredit.php文件中搞清楚。你現在這樣做的方式是非常危險和不安全的。無論如何,你可以暫時擺脫表單中的action屬性來測試你的AJAX函數。否則,它可能會覆蓋AJAX代碼。 – hesson