Let me clearly tell you what i want to accomplish in the end: I have a Php script which populates a dropdownlist using MySql db connect. Here is the code for PHP:
Start of PHP file
<!DOCTYPE html>
<head>
<script>
</script>
</head>
<body>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<label><b>Location : </b></label>
<select name="loc" id="location-dropdown">
<?php
$host="****"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="*****"; // Database name
$tbl_name="****"; // Table name
$myid="2";
//$myid=$_GET["id"];
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$data = mysql_query("SELECT * FROM $tbl_name WHERE id='$myid'") or die (mysql_error());
while($info = mysql_fetch_array( $data ))
{
$loc=$info['location'];
echo "<option value = $loc>$loc</option>";
}
$p=shell_exec("AS.pl $myid ");
?>
</select>
<div id="iamdiv"></div>
</form>
</body>
</html>
**End of PHP file**
The contents of AS.pl:
Start of perl
#!/usr/bin/perl
use DBI;
use Data::Dumper;
use JSON;
$id = "$ARGV[0]";
$dbh = DBI->connect('dbi:mysql:****','****','****')
or die "Connection Error: $DBI::errstr
";
$sql = "SELECT * FROM details WHERE id='$id'";
$sth = $dbh->prepare($sql);
$sth->execute
or die "SQL Error: $DBI::errstr
";
while (my @row=$sth->fetchrow_array)
{
push (@loci,$row[2]); ## @loci is an array of locations like america,africa etc
}
$sth->finish;
## I have another array @values which contains numbers so i will just print out both the arrays for ur ref:
for($i=0;$i<=$#values;$i++)
{
print "$loci[$i] :$values[$i]
";
}
The outputs
Singapore :2300
Amsterdam :2300
Mumbai :2300
Gurgaon :2300
i have a hash which is encoded to JSON
my %hash; @hash{@loci} = @values; my $json_text = encode_json(\%hash);
**Hash output**
$VAR1 = {
'Gurgaon' => '2300',
'Singapore' => '2300',
'Mumbai' => '2300',
'Amsterdam' => '2300'
};
JSON output print $json_text; {"Gurgaon":"2300","Singapore":"2300","Mumbai":"2300","Amsterdam":"2300"}
**End of perl**
So basically what i want to do is: 1)The returned JSON contains locations as keys and values as values.I need to compare if the location values on the location dropdown and Json location values are same then i need to return the corresponding JSON value in the div id="iamdiv". I would need to use the onchange event of the dropdown i guess,but how to use the json returned by perl here in javascript?