我想通过$ .post传递查询结果。
function GetAllTasks()
{
$sql = "select t.id as task_id,
description,
createdat,
createdby,
max_requests,
max_duration,
j.name as job_name
from darkfuture.tasks t, darkfuture.jobs j
where t.job_id = j.id";
$sqlresult = mysql_query($sql)
or die("The list of works failed: ".mysql_error($this->con));
$result = array();
while($row = mysql_fetch_assoc($sqlresult))
{
$task = new TasksResult();
$task->id = $row["task_id"];
$task->description = $row["description"];
$task->createdat = $row["createdat"];
$task->createdby = $row["createdby"];
$task->max_requests = $row["max_requests"];
$task->max_duration = $row["max_duration"];
$task->job_id = $row["job_name"];
array_push($result, $task);
}
mysql_free_result($sqlresult);
return $result;
}
以下是我的称呼方式:
$tasksDB = new TasksDB();
$tasks = $tasksDB->GetAllTasks();
现在我想通过这里传递$ tasks:
$.post("views/insert_tasks.php",{'tasks[]': $tasks}, function(data)
{
});
我知道这个{'任务[]':$ tasks}这是错的,但我不知道怎么做。
答案 0 :(得分:0)
{tasks:
[id: 42, title: 'a'],
[id: 43, title: 'b'],
...
}
答案 1 :(得分:0)
<script type="text/javascript">
$.post("views/insert_tasks.php", {tasks:<?php echo json_encode($tasks); ?>}, function(data) {
});
</script>
在旁注中,您可能需要查看mysql_fetch_object($result, 'TasksResult');
答案 2 :(得分:0)
它正在运行,我将在此处粘贴其他用户也可以使用它的所有代码。
这是获取单击复选框和查询结果对象的javascript函数,并将它们传递给.php文件:
<script type="text/javascript">
// Attach a click handler
$(document).ready(function()
{
var clickedRows=new Array();
var table = document.getElementById('tablesorter');
var rowCount = table.rows.length;
var index = 0;
$('#request_tast').click(function()
{
for(var i = 0; i < rowCount; i++)
{
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(chkbox != null && chkbox.checked == true)
{
clickedRows[index] = i;
index++;
}
}
var clickrows = JSON.stringify(clickedRows);
$.post("views/insert_tasks.php",{ clickedRows : clickrows , <?php echo "tasks:'" . json_encode($tasks) . "'"; ?> }, function(data)
{
});
});
});
</script>
这是接收单击的复选框和查询结果对象的php文件:
<?php
session_start();
require_once("../database/db_connect.php");
if(isset($_POST['clickedRows']))
{
$clickedRows = json_decode($_POST['clickedRows']);
$tasks = json_decode($_POST['tasks']);
foreach($clickedRows as $i)
{
$task_id = $tasks[$i]->task_id;
$myFile = "debug.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, $_POST['tasks']);
fclose($fh);
$user_id = $_SESSION['id'];
$requestedat = date('l jS \of F Y h:i:s A');
$requestedby = $_SESSION['first'] . " " . $_SESSION['last'];
$sql ="INSERT INTO
darkfuture.users_tasks
(
`task_id`,
`user_id`,
`requestedat`,
`requestedby`
)
VALUE (
$task_id,
$user_id,
'$requestedat',
'$requestedby'
)";
$res = mysql_query($sql) or die(mysql_error());
}
}
?>
这是我正在为我的游戏网站进行任务管理的功能,团队成员可以通过选中复选框来选择表格中的多个任务,并且任务将添加到他们的个人资料中,其他任何人都无法工作它们。