我对堆栈溢出和一个相当新的开发人员完全不熟悉,我遇到了我的第一个ajax函数,不幸的是,虽然我已经阅读了大约20个帖子但我仍然无法弄清楚我需要如何实现我的函数。一旦我知道如何执行一项功能,我通常可以获得其他任何功能。
我正在构建一个“任务”系统,我在php中有2个函数,1个列出了所有过期任务,另一个删除了任务函数,
任务列在bootstrap alert div中,基于优先级和截止日期,在每个警报中我有2个按钮,1个解雇,2个完成,我在php中有一个函数来标记状态为“2”的任务”。
这是我到目前为止所拥有的。
这是我的第一个完美运作的函数所生成的
<div class='alert alert-danger fade in'>
<strong>Task: </strong><?php echo $row[1];?> <strong class="myAlerts">Note: </strong><?php echo $row[5];?><strong class="myAlerts">Due Date: </strong><?php echo $row[2];?>
<div class='pull-right'>
<button class='close' style='margin-right: 10px;' data-dismiss='alert' aria-label='dismiss'>Completed
<span class='glyphicon glyphicon-ok-circle'></span>
</button>
<button action="removeTask()" class='close' style='margin-right: 10px;' data-dismiss='alert' aria-label='dismiss'>dismiss
<span class='glyphicon glyphicon-remove-circle'></span>
</button>
</div>
</div>
这是执行任务完成的php代码,位于Functions / functions.php
中我的dashboard.php调用函数ODTasks(),下面是我需要通过单击完成的按钮调用的函数(不需要离开模态或页面)
function removeTask($TaskName,$taskID){
if (isset($TaskName)){
echo "Taskname is set";
}
else {
header ('Location: index.php?error="Task completion variables not set [E/1]"');
}
if (isset($TaskID)){
echo "TaskID is set";
}
else {
header ('Location: index.php?error="Task completion variables not set [E/2]"');
}
if (file_exists('../database/mysqli_connection.php')){
require('../database/mysqli_connection.php');
} else {
require('database/mysqli_connection.php');
}
$updateTask = "UPDATE MC_Tasks SET status='2' WHERE TaskName ='".$TaskName."' AND TaskId='".$taskID."'";
$completedTaskResult = mysqli_query($mysqli_connection,$updateTask);
if (mysqli_affected_rows($mysqli_connection) > 0){
echo "task Completed!";
} else {
echo "Task could not be removed, lets check the query on what went wrong";
echo $updateTask;
}
}
这就是我对AJAX的测试
<script type="text/javascript">
function removeTask() {
$.ajax({
type: "POST",
url: '<?php Thisurl();?>',
data:{action:'removeTask'},
success:function(html) {
alert(html);
}
});
}
</script>
将此添加到dashboard.php 但我没有得到回音:(
<?php
//added for removing a task
if($_POST['action'] == 'removeTask') {
echo "try removing this task";
/* will soon be calling the function with this once the echo works
removeTask($TaskName,$taskID);
*/
}
?>
答案 0 :(得分:0)
删除按钮操作并添加onClick
<button onClick="removeTask()" class='close' style='margin-right: 10px;' data-dismiss='alert' aria-label='dismiss'>dismiss
<span class='glyphicon glyphicon-remove-circle'></span>
</button>