Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re^2: How do I mix up Perl and jQuery (for beginners)

by eyekona (Acolyte)
on Aug 22, 2013 at 08:12 UTC ( [id://1050485]=note: print w/replies, xml ) Need Help??


in reply to Re: How do I mix up Perl and jQuery (for beginners)
in thread How do I mix up Perl and jQuery (for beginners)

Yes this is exactly what i was doing till now - print html - so this is called CGI?

I was wondering how i can get something from perl to an existing html page but could not find a way, but it seems this way is called json... some of the others mentioned it and it is also in your example. So thank you very much for posting this. I will try it out and see if i unterstand how it works. :-)

Update: Works perfectly and seems to be the best approach. Just have one question to get it to work with my own existing database

How is this structure

return [ { username => "paco", fish => "Sunfish", }, { username => "YourUncle", fish => "Coelacanth", }, { username => "hiragana", fish => "Monkfish", }, { username => "MosaicNL", fish => "Sixgill Shark", }, ];

called? Thats not a normal array - what's the name of it?

Replies are listed 'Best First'.
Re^3: How do I mix up Perl and jQuery (for beginners)
by Your Mother (Archbishop) on Aug 22, 2013 at 14:24 UTC

    How is this structure called?

    That's an (anonymous, no named variable) array reference which holds hash references. See perlref, and really! Do read it.

    use JSON; use Data::Dump "dump"; dump( other_way() ); # This will show the anonymous structure. print "JSON: ", to_json( other_way() ), $/; sub other_way { my %user1 = ( username => "paco", fish => "Sunfish" ); my %user2 = ( username => "YourUncle", fish => "Coelacanth" ); my %user3 = ( username => "hiragana", fish => "Monkfish" ); my %user4 = ( username => "MosaicNL", fish => "Sixgill Shark" ); my @users = ( \%user1, \%user2, \%user3, \%user4 ); return \@users; }

    NB: the dump() can appear "out of order" because it goes to STDERR. Output-

    [ { fish => "Sunfish", username => "paco" }, { fish => "Coelacanth", username => "YourUncle" }, { fish => "Monkfish", username => "hiragana" }, { fish => "Sixgill Shark", username => "MosaicNL" }, ] JSON: [{"fish":"Sunfish","username":"paco"},{"fish":"Coelacanth","user +name":"YourUncle"},{"fish":"Monkfish","username":"hiragana"},{"fish": +"Sixgill Shark","username":"MosaicNL"}]

    You can see in &other_way above how much more terse and more clean anonymous structures tend to be.

      Another thank you very much. Now i was able to change your script to do my biddings with my database. I owe you one. :-) And yes after this monday meeting i will definitly read the references and tell my boss he won't be happy with the result programm if he don't give me time to learn...
Re^3: How do I mix up Perl and jQuery (for beginners)
by poj (Abbot) on Aug 22, 2013 at 10:26 UTC

    Here's a very simple demo of an auto-complete input field using html/mysql/json

    HTML file
    <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>autocomplete demo</title> <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/s +moothness/jquery-ui.css"> <script src="http://code.jquery.com/jquery-1.9.1.js"></script> <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script> </head> <body> <label for="autocomplete">Select : </label> <input id="autocomplete"> <script> $( "#autocomplete" ).autocomplete({ source: "http://localhost/test/data.cgi", }); </script> </body> </html>
    data.cgi to provide data
    #!/usr/bin/perl use CGI; use DBI; use JSON; my $dbh = get_dbh(); # your connect details # test data $dbh->do('CREATE TEMPORARY TABLE json ( col1 char(3))'); for ("ABC", "BCD", "CDE", "DEF", "EFG", "FGH", "GHI"){ $dbh->do('INSERT INTO json VALUES (?)',undef,$_); } # query database my $q = CGI->new(); my $sth = $dbh->prepare('SELECT * FROM json WHERE col1 like ?'); $sth->execute('%'.$q->param('term').'%'); my @data=(); while ( my @f = $sth->fetchrow_array){ push @data,$f[0]; } print "Content-type: application/json; charset=iso-8859-1\n\n"; print JSON::to_json(\@data);
    poj

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1050485]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-03-28 13:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found