tags:

views:

48

answers:

4

Hi,

I want to pass the result of a query trough a $.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;

}

Here is how i call it:

$tasksDB = new TasksDB();
$tasks = $tasksDB->GetAllTasks();

Now i want to pass $tasks through here:

$.post("views/insert_tasks.php",{'tasks[]': $tasks}, function(data)
    {

});

I know this {'tasks[]': $tasks} it's wrong but i don't know how to do it right. Some help will be appreciated. Thanks in advance!

A: 
{tasks:
    [id: 42, title: 'a'],
    [id: 43, title: 'b'],
    ...
}
zerkms
A: 
<script type="text/javascript">
$.post("views/insert_tasks.php", {tasks:<?php echo json_encode($tasks); ?>}, function(data) {

});
</script>

On a side note you might want to look at mysql_fetch_object($result, 'TasksResult');

Petah
Or possibly using something aside from the ancient mysql_ library, like mysqli or PDO...
El Yobo
With this code gave me the following error:invalid porperty idi got something like this in debug:<script type="text/javascript"> $.post("views/insert_tasks.php", [{"id_task":"1","name":"Miguel"},{"id_task":"2","name":"Robert"}], function(data) { }); What i'm doing wrong?</script>
I edited the code from the original post, try that instead.
Petah
A: 

With this code gave me the following error: invalid porperty id

I got something like this in debug:

<script type="text/javascript"> $.post("views/insert_tasks.php", [{"id_task":"1","name":"Miguel"},{"id_task":"2","name":"Robert"}], function(data) { }); </script>

What i'm doing wrong?

A: 

It's working, i will paste here all the code that way other users can use it as well.

This is the javascript function that gets the clicked checkboxes and the query result object and passes them to the .php file:

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

This is the php file that receives the clicked checkboxes and the query results object:

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

This is a feature I'm doing for my game website for task management, the team members can choose several tasks in a table by checking the check boxes and that tasks will be added to their profile and no one else can work on them.