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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:11:21+00:00 2026-06-03T02:11:21+00:00

I looked for this in previous threads so apologies if has been answered. My

  • 0

I looked for this in previous threads so apologies if has been answered. My javascript is the following, where the data parameter is a string which refers to a table in a MySQL database:

    getData("UKnatgas");

function getData(data){
$.getJSON("service.php?action="+data, function(json) {              
    $.each(json.UKnatgas,function() {
       var info = this.UKnatgas;                    
                $('#UKnatgas').append( info );
    });
});
}

Firstly, I want to pass the string “UKnatgas” into the sub $.each function so that it would read:

$.each(json.data,function() {
       var info = this.data;                    
                $('#data').append( info );
    });

But this doesn’t work? Could you tell me why?

The second part is getting rid of all instances of “UKnatgas” in the php file:

  if($_GET){
if($_GET['action'] == 'UKnatgas'){
    $query = "SELECT UKnatgas FROM UKnatgas order by Date DESC Limit 1 ";
    $result = db_connection($query);

    $UKnatgas = array();

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        array_push($UKnatgas, array('UKnatgas' => $row['UKnatgas']));
    }
    echo json_encode(array("UKnatgas" => $UKnatgas));
    exit;
    }
}

So to summarise, I’d like to initialise the function getData(data) with a string which is propagated into the javascript function and then the php file. Is this possible?

Thanks!

  • 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-06-03T02:11:22+00:00Added an answer on June 3, 2026 at 2:11 am

    The PHP is easy, although I would allow for some type of check for errors and maybe even check for hack attempts as you are putting a user editable string. This may allow for MySQL Injection so be careful.

    If you can, use mysql_real_escape_string when ever possible. See link for more information: http://www.php.net/manual/en/function.mysql-real-escape-string.php

    if($_GET['action']){
        $query = "SELECT `".$_GET['action']."` FROM `".$_GET['action']."` order by Date DESC Limit 1 ";
        $result = db_connection($query);
    
        $actionArray = array();
    
        while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            array_push($actionArray, array($_GET['action'] => $row[$_GET['action']]));
        }
        echo json_encode(array($_GET['action'] => $actionArray));
        exit;
    }
    

    And not sure exactly but this should work for the JSON

    $.each(json, function(key, val) {
        $('#'+data).append( val );
    });
    

    http://api.jquery.com/jQuery.getJSON/

    I tested it in your case and you will need to do the following to access the data. Now this is using JSON as an Associative Array which it isn’t, but not 100% how to do it otherwise.

    function getData(data) { 
        $.getJSON("service.php?action="+data, function(json) { 
            $.each(json[data], function() { 
                var info = this[data];
                $('#'+data).append( info ); 
            }); 
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(This question is following on from my previous problem which has been fixed (Here)
Okay, I know this question has been asked before: Previous Question I have also
I looked at previous post based on this but they do not relate. I
I looked at this: Returning values from nested functions in Javascript but it did
I looked at this example http://msdn.microsoft.com/en-us/library/bb399420.aspx and clicked on DataContext.CreateDatabase which brought me to
I've looked but didn't find previous questions specific enough, so sorry if this is
I've looked and tried the solutions of previous questions on this topic ( here
Ok guys I know this question has been asked before but I am very
This may be an easy one. I looked through previous questions (and other places
This question has been previously mentioned on Stack Overflow, but the answers were not

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.