1

I need to pass valuesArray to php if button is clicked, when anotherButton is clicked, i want to print this array in PHP.

home.js:

button.onclick = function (){

    valuesArray = ['a', 'b', 'c']

$.post('index.php', {data: valuesArray});
}

index.php:

<?php
    If(isset($_POST['anotherButton'])){
        $getData = $_POST['data'];
        print_r($getData);
    } 
?>

If button gets clicked I get

undefined index: data

which should mean that data wasn't passed to PHP.

1
  • Try to use different index name (not data) Commented Mar 23, 2017 at 14:42

2 Answers 2

1
$.post('index.php', {"data": valuesArray},function(return,result){});

Notice the double quote around "data".
Also may need to json encode valuesArray?

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

Comments

0
$.post('index.php', valuesArray, function(result) {
    // ....
});

Have a look at documentation: https://api.jquery.com/jquery.post/

Comments

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.