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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T11:46:50+00:00 2026-05-21T11:46:50+00:00

I have connected to a mysql database and I am trying to use jquery

  • 0

I have connected to a mysql database and I am trying to use jquery to populate a drop down menu using data from the database and JSON.

I do not have much experience with JSON, but I have read many articles and searched the web for tutorials.

In my PHP to connect to the database, the last lines format and output the JSON correctly:
$response = $_GET["jsoncallback"] . "(" . json_encode($colors) . ")";
echo $response;

In the javascript, I use:
$.getJSON("db.php?jsoncallback=?", function(data){ //loop that populates the drop down });

I have jquery set up to grab the JSON and populate the drop down, which is also working correctly. However, the JSON data that is created in the PHP is outputted at the top of my generated page.

i.e.,
([{"color":"Purple"},{"color":"Red"},{"color":"Blue"},{"color":"Pink"},{"color":"Yello"}])

How do I still access the jsoncallback without outputting the JSON on my page?

  • 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-21T11:46:50+00:00Added an answer on May 21, 2026 at 11:46 am

    It looks like you are using the same file to generate the JSON output aswell and generate your normal pages, which is why the JSON will output at the top of the page.

    Try wrapping the JSON output in an if statement that prevents output unless you reqire it, ie when you make an AJAX request, or move the JSON output to another file such as ajax.php that is specifically for handling AJAX requests. Simple example below.

    Updates JS, passing over a new actions param.

    $.getJSON("ajax.php?jsoncallback=?", { action: 'get_colors' },  function(data){ //loop that populates the drop down })
    

    New PHP file ajax.php

    // Include required other php files, etc
    switch ($_GET['action']) {
    
        case 'get_colors':
            $response = $_GET["jsoncallback"] . "(" . json_encode($colors) . ")";
            echo $response;
        break;
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use javascript, without framework(prototype, jQuery,etc), to insert data passed from
I am trying to use sqlalchemy to connect with mysql database. I have set
I have been trying to display image from database using php but i am
I have successfully connected to an Oracle database (10g) from C# (Visual Studio 2008)
I have a JPA project connected to a MySQL database where my entity object
I have a couple of scripts that interact with a MySQL database using ActiveRecord
I'm trying to use JSON, I'm getting the information from the server so I've
I am trying to retrieve data from MySQL for a flash application via PHP,
I have connected to a server via SFTP using FileZilla and accepted adding the
I have an ASP .NET 2.0 website connected to a SQL Server 2005 database.

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.