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

108
Views
Get PHP object results using AJAX

I have a checkbox and the user checks it in, an AJAX call should be sent to a PHP file which returns an object. I have a SQL statement which returns data and the AJAX should get it from the PHP.

For instance the SQL returns 2 rows:

$sql = "SELECT * FROM table WHERE 1";
    if ($result = mysqli_query($con, $sql)) { $num = mysqli_num_rows($result); $arr = array();
        if ($num > 0) { $object = new stdClass();
            while ($bsi = $result-> fetch_assoc()) { 
                $object->id = $bsi['id']; $object->name = $bsi['name'];
                array_push($arr, $object);
            } die(json_encode($arr)); 
        }
    }

// Create an array, create an object. save fetched data into an object, then add the object into an array.

Let's say the above statement returns the 2 rows: [0: {id: 1, name: "name1"}, 1: {id: 2, name: "name2"} ]

AND THE AJAX CALL

$.ajax({
  enctype: "multipart/form-data", type: "POST", url: "path", dataType: 'json', contentType: false, processData: false,
  success: function(data) { console.log(data); }

BUT the console.log will display this: [0: {id: 1, name: "name1"}, 1: {id: 1, name: "name1"} ] It always shows the first element of the array.

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