Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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

by Your Mother (Canon)
on Aug 21, 2013 at 15:49 UTC ( #1050381=note: print w/ replies, xml ) Need Help??


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

While this might be just as confusing as things you've seen or tried, it's minimal and available: CGI/Ajax example: lookup a value when a field is filled in.

The main problem with web development isn't that any part of it is hard, it's not, it's that there are so many moving parts. Writing dynamic forms / ajax stuff + the backend is not a beginner task, even for someone seasoned in other programming. You might be feeling lost and like Perl is a wasteland but I assure you it's one of the best for web work; as is jQuery. If you post code you are working on, you'll probably get good help.

One thing you said leads me to believe you are using CGI and doing this kind of thing-

print "Content-Type: text/html\r\n\r\n", print "<html>...this"; print "that...</html>";

There are many ways to get output out in Perl. The CGI.pm proper module is quite old-school. It's not a first choice for production but sometimes can be for one-offs and learning as long as you're aware better (more robust, testable, and reusable) approaches exist. Still, it's serviceable and stronger than most give it credit. You can, e.g., one page, one print-

use strict; use warnings; use CGI ":standard"; print header(), start_html("OHAI"), h1("Bingo"), blockquote( p([ "one", "two", "three" ]), ), end_html();


Comment on Re: How do I mix up Perl and jQuery (for beginners)
Select or Download Code
Re^2: How do I mix up Perl and jQuery (for beginners)
by eyekona (Acolyte) on Aug 22, 2013 at 08:12 UTC

    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?

      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

      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...

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (18)
As of 2014-12-18 14:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (56 votes), past polls