Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
Can I post an JS array to php via ajax, if button is pressed?

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.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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

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

about 4 years ago · Santiago Trujillo Report

0

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

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

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!