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

The Archive Base Latest Questions

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

I am trying to send a variable with jQuery’s getJSON function but it doesn’t

  • 0

I am trying to send a variable with jQuery’s getJSON function but it doesn’t seem to be going through, I am also unsure of how to get the variable I have just sent in the waiting PHP file, do I just reference it via the name I sent it as, for instance if I send a variable called 'test', can I get it like this, $_POST['test'] on the other side? or how exactly would that work?

I am trying to populate a drop down list using the method below, any advice on improving the code would be greatly appreciated!

Here is what the PHP returns:

[{"city":"One"},{"city":"Two"},{"city":"Three"},{"city":"Four"}]

jQuery:

//get the cities
$("#province").live('change', function(){
    var test = $('#province').val();
    //alert(test);

    $.getJSON("cities.php", test, function(data){

    //clean out the select list
    $('#city').html('');

        //run the loop to populate the drop down list
        $.each(data, function(i, data) {
            var city = data.city;     

            $('#city').append(
                $('<option></option>').html(city)
            );
        });
    });
});

PHP:

$province = $_POST['test'];

//mySQL query here

$myarray = mysql_fetch_array($query, true);
$string = explode(',', $myarray['cities']);

foreach($string as $key=>$value) {
    $string[$key] = array('city'=>$value);
}

$json = json_encode($string);
echo $json;

What could I be doing wrong?

Thanx in advance!

  • 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-16T20:39:05+00:00Added an answer on May 16, 2026 at 8:39 pm

    A couple of things, first, getJSON is a get ajax call. So, you should be expecting the data you pass using getJSON to end up in $_GET and obviously $_REQUEST, but not $_POST. Secondly, you have to pass an object which describes a map of data which will be reflected in the associative array, $_GET.

    So your code should read…

    $.getJSON("cities.php", { test: $('#province').val() }, function (data) {
    

    with that, you can access on PHP side as $_GET['test']

    A few improvements I can give… (untested)

    //get the cities
    $("#province").live('change', function(){
    
        $.getJSON("cities.php", { test: $('#province').val() }, function(data){
    
            if(!data.ok) {
                alert('Error getting cities. Please try again later.');
            }
    
            var markup = '', len = data.cities.length;
    
            for(var i = 0; i < len; i++) {
                markup += '<option>' + data.cities[i] + '</option>';
            }
    
            $('#city').html(markup);
    
        });
    
    });
    

    and php

    $province = $_POST['test'];
    
    //mySQL query here
    
    $myarray = mysql_fetch_array($query, true);
    $cities = explode(',', $myarray['cities']);
    
    echo json_encode(array(
        'ok' => true,
        'cities' => cities
    ));
    

    Good luck.

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

Sidebar

Ask A Question

Stats

  • Questions 538k
  • Answers 538k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to break this down into two steps: 1)… May 17, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer For your recordset, you would want to use the following… May 17, 2026 at 2:05 am
  • Editorial Team
    Editorial Team added an answer Ordinarily you can develop applications (but not hack on android… May 17, 2026 at 2:05 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am using jQuery and trying to load a variable in place of a
I am trying to return a variable from onreadystatechange and seem to be having
I am trying to send a name:value pair to a php script using the
I am trying to use jquery to do a client side validation to do
I'm trying to get a javascript script (running in a Safari extension, to be
Does JQuery AJAX have a different User Session than a postback to the same
Sorry folks but I asked a question earlier and perhaps did not explain myself
I am learning how to use JQuery to help check data availability. To start
I'm trying to implement a starring system akin gmail, being able to mark an
I am trying to make an asynchronous request with POST method from a web

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.