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

  • Home
  • SEARCH
  • 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 1043417
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:39:49+00:00 2026-05-16T15:39:49+00:00

I am trying to create a drop down list using jQuery, PHP and mySQL,

  • 0

I am trying to create a drop down list using jQuery, PHP and mySQL, here is my code thus far,

HTML:

<select id="box"></select><br />
<input id="button" type="button" value="populate" />

Javascript/jQuery:

$(document).ready(function() {
    $("#button").click(function (){     
        $.getJSON("test_post.php", function(data){

            $.each(data, function(user) {
                $('#box').append(
                    $('<option></option>').html(user)
                );
            });
        });
    });
});

PHP:

mysql_connect('localhost', 'user', '1234');
mysql_select_db('json');
$test = mysql_query('SELECT user,pass FROM login WHERE user="john"');

while($row = mysql_fetch_array($test, true)) {
    $data .= json_encode($row);
};

echo $data;

I have 3 entries in the database, and when I run the ‘test_post.php’ file, it echoes out the JSON, but when I try and get it to populate the drop down, nothing happens!

Any idea why?

  • 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-16T15:39:50+00:00Added an answer on May 16, 2026 at 3:39 pm

    There are several problems in your code.

    First: attempt to use undefined variable $data, you should have initialized it to an empty string before while loop: $data = '';, otherwise this can send PHP notice with the JSON response, depending on values of display_errors and error_reporting settings.

    Second: as @shamittomar said, $data must be an array and json_encode() must be called only once for the whole array. For now, you’re sending several JSON objects concatenated to a single string, which is wrong.

    Third: value of the user function parameter in the JavaScript is actually an index of the data array, not a value, but even if user would be a value, it would be a JavaScript object with user and pass properties, not a string.

    Resulting code should be (changed parts only):

    PHP

    $data = array();
    while ($row = mysql_fetch_array($test, true)) {
        $data[] = $row;
    };
    
    echo json_encode($data);
    

    JavaScript

    $.each(data, function(i, user) {
        $('#box').append(
            $('<option></option>').html(user.user)
        );
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a select drop-down list using php. Every time i
I am trying to create a drop down list using asp.net mvc. Model: public
I'm trying to create a custom drop down and using the code below it
I am trying to create a drop down menu using CSS and HTML, it
I'm trying to make a drop down table using jQuery, with a similar code
I'm trying to create a drop down list that populates from a database. I
I'm trying to create a drop down list in an ASP.NET MVC3 view based
I am trying to create a drop down list that incorporates database data and
I am trying to create a drop down list i have it working but
I am trying to create a drop down list. I have it working but

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.