0

I am trying to pass two arrays that I have created in JavaScript to another php page. I have researched this and I don't know what I am doing wrong. I have been following many forums and tutorials but I can't seem to get mine to work. I have a form which you can add additional lines which is why I have arrays. When the user presses submit on "process.php," this function is called:

$("#submit").click(function() {
    var accosArray = new Array();
    accomp = $("#TextBoxDiv1 textarea[name=sacco]").val();
    accosArray.push(accomp);
    alert(accosArray[0]);
    for (var i = 2; i < counter; i++) {
        accomp = $("#TextBoxDiv1" + i + " textarea[name=sacco]").val();
        accosArray.push(accomp);
        alert(accosArray[i - 1]);
    }
    var tasksArray = new Array();
    taskSelect = $("#TextBoxDiv1 select[name=lstDropDown_A]").val();
    if (taskSelect == "") {
        //If user entered a task
        taskOther = $("#TextBoxDiv1 input[name=textboxoption_A]").val();
        tasksArray.push(taskOther);
        alert(tasksArray[0]);
    } else {
        tasksArray.push(taskSelect);
        alert(tasksArray[0]);
    }
    for (var i = 2; i < counter; i++) {
        taskSelect = $("#TextBoxDiv1" + i + " select[name=lstDropDown_A]").val();
        if (taskSelect == "") {
            //If user entered a task
            taskOther = $("#TextBoxDiv1" + i + " input[name=textboxoption_A]").val();
            tasksArray.push(taskOther);
            alert(tasksArray[i - 1]);
        } else {
            tasksArray.push(taskSelect);
            alert(tasksArray[i - 1]);
        }
    }
    $.post('127.0.0.1/Working Files/Best Files/In Progress/status.php';, {
        task: tasksArray
    }, function(result) {
        alert(result[0]);
    }, 'json');
});

This puts the user inputs into the arrays and then I try to use the $.post method at the end in order to be able to pass the arrays to the next page but I am not sure if the syntax is correct.

Then it is passed to the next file, "status.php," which at the beginning states:

<?php
 session_start();
 $task=$_POST['task'];
 echo json_encode($task);
?>

The echo shows as "null."

Please let me know what I am doing wrong!

Thanks in advance!!

5
  • Your $.post line contains a misplaced semicolon. Commented Jul 11, 2012 at 18:52
  • removed now. still echos "null" Commented Jul 11, 2012 at 19:03
  • There are several steps involved, the bug could be in any of them. I personally would use Firebug or a similar tool to look at the actual request and response data that is being exchanged. And use console.log instead of alert(), because it can display more than just plain strings. Commented Jul 11, 2012 at 19:14
  • Do an echo of $_POST['task']. And at most it will be json_decode($task) not encode. Commented Jul 11, 2012 at 19:22
  • So I am using firebug and I found where the bug is but I don't understand it. This is what is says: POST .../status.php and jquery (line 8240). The line from jquery says: xhr.send( ( s.hasContent && s.data ) || null ); Any ideas? Commented Jul 11, 2012 at 20:12

1 Answer 1

0

Looks like a rogue semicolon at the very least:

$.post('127.0.0.1/Working Files/Best Files/In Progress/status.php';

Remove that semicolon for starters...

2
  • removed the semicolon. still null. btw I am very new to javascript so sorry for my messy code. Commented Jul 11, 2012 at 19:02
  • You'll also need to put quotes around your JSON parameter: "task" : tasksArray Commented Jul 11, 2012 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.