我正在开发一个消息系统,在该系统中,用户向另一个用户提交一个响应。然后,接收者在while循环中查看他的所有消息。我创建了一个删除按钮,它将删除删除按钮链接到的任何消息。我有两个问题。首先,删除按钮不工作,其次,当它工作时(它不再工作),while循环将所有删除按钮链接到第一条消息,而不是单独链接到while循环产生的每条消息。此外,我知道mysql已被弃用。将很快完成过渡。
这是第一个代码:
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script type="text/javascript" >
function load(thefile, thediv) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject ('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', thefile + key, true);
xmlhttp.send();
}
</script>
<?php
while($ergo_data = mysql_fetch_assoc($ergo_query_result)) {
echo 'Message: ' . $ergo_data['ergo'] . '<br>';
echo 'From: ' . username_from_user_id($ergo_data['user_id_seeker']) . '<br>';
echo 'Time submitted: ' . $ergo_data['ergo_time_submitted'] . '<br><br><br><br>';
echo $ergo_data['primary_key'];
?>
<div class="changes">
<input type="button" value="delete" class="<?php echo $ergo_data['primary_key']; ?>"
name="<?php echo $ergo_data['user_id_seeker']; ?>"
onclick="load('ajax_ergo_list.php?key=',
'delete');">
</div>
<script type="text/javascript" >
$(document).ready(function(){
var key = $(".changes :input").attr("class");
alert(key);
});
</script>
<br>
<br>
<br>
<?php
}
?>
<div id="delete"></div>
这是第二个文件,包含按下按钮时我想要发生的事情。
if (isset($_GET['key'])) {
$key = sanitize($_GET['key']);
}
if (!empty($key)) {
$query = "DELETE FROM `ergo` WHERE `primary_key` = '$key'";
$query_run = mysql_query($query);
echo 'Deleted!';
}
?>
好吧,首先,这是你在这里进行的各种疯狂的代码。。。
<script type="text/javascript" >
$(document).ready(function(){
var key = $(".changes :input").attr("class");
alert(key);
});
</script>
您的脚本位于while循环中。这将提醒循环的次数。这就是函数所做的全部。它没有设置全局变量或任何东西。
关于jQuery,请使用它:
function load(thefile, thediv) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject ('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById(thediv).innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', thefile + key, true);
xmlhttp.send();
}
浓缩为:
function load(thefile, thediv) {
$.get(thefile+key,function(responseText){
$('#'+thediv).html(responseText);
});
}
关于您关于删除功能的问题:
<div class="changes">
<input type="button" value="delete" class="<?php echo $ergo_data['primary_key']; ?>"
name="<?php echo $ergo_data['user_id_seeker']; ?>"
onclick="load('ajax_ergo_list.php?key=',
'delete');">
</div>
您的onclick
是正在启动的javascript。你把第一个变量设置为链接,第二个设置为我猜的动作?在函数代码中,第二个变量应该是显示信息的div。key
找不到。试着这样做:
<div class="changes">
<input type="button" value="delete" class="<?php echo $ergo_data['primary_key']; ?>"
name="<?php echo $ergo_data['user_id_seeker']; ?>"
onclick="load('ajax_ergo_list.php?key=<?php echo $ergo_data['primary_key'];?>',
'delete');">
</div>
以及你的加载函数:
function load(thefile, thediv) {
$.get(thefile,function(responseText){
$('#'+thediv).html(responseText);
});
}
祝你好运!