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

131
Views
Get associative array after post via ajax

I need to send a form and a array/string to the server.

My Ajax looks like this:

 var formData = $("#formpv").serialize();
        $.ajax({    
            type: 'POST',
            url: 'scripts/formIntoDB.php',
            data: formData,

In this way I can retrieve the form data in PHP like this:

$_POST['name'];
//Output "Foobar"

But when I send the the serialized formdata and another param like this:

 var formData = $("#formpv").serialize();
 var posData = "TEST";

        $.ajax({    
            type: 'POST',
            url: 'scripts/formIntoDB.php',
            data: {form: formData, pos: posData},

And try to get name now in PHP:

$_POST['form']['name'];

This just throws PHP Parse error: syntax error, unexpected '['

Whats the difference when I send the data like this data: {form: formData, pos: posData} and data: formData ?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The issue is because the string in formData becomes double-wrapped, ie. a string of key/value pairs within a string of key/value pairs. As only the top level of this is deserialised, your PHP code cannot read the second level in the manner you intend.

To fix this you either need to manually create the entire object, eg:

$.ajax({    
  type: 'POST',
  url: 'scripts/formIntoDB.php',
  data: {
    form: {
      name: $('#yourNameInput').val(),
      // all your other inputs here...
    }, 
    pos: posData
  }
});

Or you instead could keep the request data flat and just add pos using a hidden input within the form.

about 4 years ago · Juan Pablo Isaza 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!