salamey
salamey

Reputation: 3811

output json data dynamically from perl cgi

I have a simple perl cgi that i use in my extjs project, written like this :

use JSON;

print "Content-type: application/json\n\n";

my $node_hash = {
data => [{
     "Week",1145,
         "Sev_Logged", "3_major"
},{
    "Week", 1146,
        "Sev_Logged", "4_minor"
}]};
print to_json($node_hash);

Which returns data written in json format that i use to generate a chart. I also have a form which contains a combobox with values : "3_major" and "4_minor" corresponding to the "Sev_Logged" field. Now what i want is, whenever the user chooses "4_minor", my perl cgi will only return week 1146, thus my chart will only contain data of week 1146.

Is this possible? How? Thank you

Upvotes: 2

Views: 2855

Answers (2)

simbabque
simbabque

Reputation: 54323

I don't know where your data is coming from, but you can do something like this:

use strict;
use warnings;
use CGI;
use JSON;

my $q = CGI->new;
print $q->header("application/json");

my $Sev_Logged = $q->param('Sev_Logged');

my $data = [
  {
    "Week" => 1145,
    "Sev_Logged" =>"3_major",
  },
  {
    "Week" => 1146,
    "Sev_Logged" => "4_minor",
  },
];

my $node_hash = {
  data => [
    grep { $_->{'Sev_Logged'} eq $Sev_Logged } @$data,
  ]
};
print to_json($node_hash);

You can call it like this: GET script.pl?Sev_Logged=3_major


Anyway, I think if you already transmitted all the data, you should filter it on the client side using JavaScript only. There is no AJAX call necessary in my opinion.

Upvotes: 4

Joel
Joel

Reputation: 3483

I would use CGI to assist with this. In short, you need to pass the combobox field to this CGI script via a form and a GET or POST request. This CGI script then needs to read in the value of the combobox via the parameters. If you use the CGI module, you can get the parameters via the param method.

Upvotes: 1

Related Questions