0

I want to do is send the array values from a javascript variable to php using ajax.

My problem is after I click the send button and when i got to php file to see if its passed or not its empty.

But when i click the network headers the ajax send the array already.

Here is the result i see in the network after i click the send button:

Form Data:

term[0][]:awdawd
term[0][]:awdawd
term[0][]:Male
term[0][]:<button class='delete'>Delete</button>
term[1][]:awdaw
term[1][]:wda
term[1][]:Female
term[1][]:<button class='delete'>Delete</button>
term[2][]:awdawd
term[2][]:awdawd
term[2][]:Male
term[2][]:<button class='delete'>Delete</button>

My php file should receive the array but its not can anyone help me.

script:

$("#saveTable").click(function(){
 $.ajax(
    {
    url: "saveTable.php",
    type: "POST",
    data: { tableArray: dataSet},
    success: function (result) {

    }
});  
});

saveTable.php:

<?php
   $tableArray = $_REQUEST['tableArray'];
  echo $tableArray;
?>
3
  • you didn't define dataSet in script... Commented Jul 30, 2014 at 13:29
  • @Shaunak Shukla dataSet is a global variable and has a value already to begin with. Commented Jul 30, 2014 at 13:32
  • ok, fine.. don't echo $tableArray, instead use print_r($tableArray) as it's an array!! Commented Jul 30, 2014 at 13:34

1 Answer 1

1

"...when i got to php file to see ..." Heres the probem. Php is stateless, visiting the php page is a new request, completely seperate from the ajax request.

To see the output from the php file in response to the ajax request, use the success callback:

success: function (result) {
    alert(result);
}

Also, as noted in comments, the dataset variable does not appear to be defined, but if you are seeing the results you suggest in network tab, i guess you just omitted that from your question

Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to see it in php page? cuz im planning to save it to a database.
the result variable in the success function is the complete output from the php file, anything you could see by visiting the file directly in the browser, you could see via this callback.
In my example i use alert, but you could instead append the result to a div, or use console.log() to see the result easier

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.