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 6468157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:51:49+00:00 2026-05-25T05:51:49+00:00

I’m still trying to get the grasp of using JSON with php and jquery.

  • 0

I’m still trying to get the grasp of using JSON with php and jquery.

I have a page which queries a database to get all users who have upcoming birthdays (ignore the sql, its just for this example) The code grabs the database return and puts it into an array, which looks something like this:

Array (
 [idnum] => Array (
   [0] => 3
   [1] => 10
   [2] => 74
 )
 [name] => Array (
   [0] => Betty Smith
   [1] => Jim Pierce
   [2] => Sam Smith
 )
)

I want to put this data into some jquery to display to the user in an html select statement or something similar, so that they can make a choice, and then retain an IDnum for future code/ajax.

My code follows, in its entirety:

<?php
session_start();

$sql_server = 'xxxx';
$sql_db = 'xxxx';
$sql_username = 'xxxx';
$sql_password = 'xxxx';

$db = mysql_connect("$sql_server", "$sql_username", "$sql_password") or die ("Error connecting to database.");
mysql_select_db("$sql_db", $db) or die ("Couldn't select the database.");

list($currentdate) = mysql_fetch_row(mysql_query("SELECT currentdate FROM calendar",$db));

/////////////////////////


$result = mysql_query("SELECT IDvalue, name FROM people where birthday => $currentdate",$db);
$rowcheck = mysql_num_rows($result);
//echo $rowcheck;
$birthdays = array();
$i = '0';
while ($row = mysql_fetch_assoc($result)){
    foreach ($row as $col => $val){
        if ($col == 'IDvalue') {
         //print " $val ";
         $birthdays['idnum'][$i] = $val;
         }
         if ($col == 'name') {
            $birthdays['name'][$i] = $val; 
         }

    }
    $i++;

   }


echo "</br></br><input type=\"button\" id=\"showbirthdays\" value=\"Show birthdays\"/>";
?>  
<html>
<head>
    </head>
    <body>

<script src="jquery.js" type ="text/javascript"></script>

    <script>
    $('#showbirthdays').click(function() {

        var jsonobj = <?php echo json_encode($birthdays); ?>;
        //alert (jsonobj) //  this does not seem to work.  just gives [object Object]

        // Here's where i'm really lost.  I would like to use jquery to display
        // a select box that shows the name of birthday people, and uses their IDnum as an identifer for further code.
        // some kind of foreach with an html.push, and then append to a div on the page...?


        });
    </script>

    </body>


</html>


}
?>

I would very much like to see exactly how to grab that array data within the jquery, and use it. Or, if someone has a suggestion of a different way to move the data, I’m interested in alternatives.

Edit:

$.each(jsonobj, function(){
            //Here `this` will point to each element of the object
            alert (this);
        });

Works great, gives me output like this: “Betty Smith,Jim Pierce, Sam Smith”.

$.each(jsonobj.name, function(){

This also works, and gives me separate output with the names,

Here’s what I’m looking for:

var html = [];
html.push('<select name="birthdaynames" id="birthdayselect">')
$.each(jsonobj, function(){
      html.push('<option name="asdf" id="this.IDnum">' + this.name + '</option>')      

        });
html.push('</select>')
$('#showbirthdays').append(html.join('')); 

How do I get those specific values using (this)?

  • 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-25T05:51:50+00:00Added an answer on May 25, 2026 at 5:51 am

    [object Object] is correct, when you are trying to alert a JavaScript object.

    Try to alert(jsonobj.idnum) or alert(jsonobj.name).

    To make a <select> you can use $.each to loop through the data.

    var $sel = $('<select name="birthdaynames" id="birthdayselect">');
    $.each(jsonobj.idnum, function(i,v){
        var name = jsonobj.name[i];
        var $opt = $('<option/>').val(v).text(name);
        $sel.append($opt);
    });
    $('#showbirthdays').append($sel);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a text area in my form which accepts all possible characters from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I am reading a book about Javascript and jQuery and using one of the
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in

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.