Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6121531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:47:40+00:00 2026-05-23T15:47:40+00:00

i have a ajax that needs to return some values, but if i use

  • 0

i have a ajax that needs to return some values, but if i use a while() to get all the result from my db, the return is “nothing”.

js:

$.post("ajax/file.ajax.php", 
  { 
    cache: false
  }, 
  function(data){ 
    alert(data.region);
  },'json'
);

php:

while(!$res->EOF)
    {
        header('Content-Type: application/json');
        $return_data = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
        echo json_encode($return_data);

        $res->MoveNext();
    }

so, how can i use the while with json ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:47:41+00:00Added an answer on May 23, 2026 at 3:47 pm

    The easiest solution would be to create an array and then array.push the row you are generating onto the end of it.

    For example:

    PHP

    $array = array();
    
    while(!$res->EOF) {
        $dataFromDB = array('value'=>''.$res->fields['VALUE'].'','region'=>''.$res->fields['REGION'].'');
        $array[] = $dataFromDB; //or array_push($array, $dataFromDB); 
    
        $res->MoveNext();
    }
    
    header('Content-Type: application/json');
    echo json_encode($array);
    

    JS:

    $.post("ajax/file.ajax.php", 
      { 
        cache: false
      }, 
      function(data){ 
        for(n in data) {
            alert(data[n].region);
        }
      },'json'
    );
    

    Then output the json encoded array to finish.

    You should also move your header out of the while loop and put it just before you echo the new array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While debugging jQuery apps that use AJAX, I often have the need to see
I used to have a simple ajax that helped me take all items from
I have a AJAX post method that gets xml data from server. But when
I understand that AJAX is asynchronous, and all that. But I have the following
I have an Ajax call that currently needs to be synchronous. However, while this
I have a AJAX request that will return a string that contains some HTML
I have a JAX-RPC (Java) web service that needs to return a complex polymorphic
I have some AJAX work that brings across an additional form element that I
I have an Ajax contact form that links to a jquery file but for
I have two combobox. I need get some value from first combobox1 after combobox1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.