how do i pass a value from AJAX or JavaScript to Perl or CGI?
-
I don't think CGI::Ajax will work. What I need to do is get the value of document.URL and store it in a Perl variable so I can parse it. If the url contains garbage, CGI.pm won't pass the whole thing, only what it recognizes. I need to be able to capture whatever is in the address bar ... If you type "blue smurf", spaces and all, I want to be able to click a button and parse the results in my Perl/CGI app. Thanks!
-
Answer:
Use Javascript to make the value of a hidden field equal to the document.URL value. Then retrieve the value of that hidden field through the normal CGI.pm param() method. <input type="hidden" id="url" name="url" value=""> <input type="submit" name="submit" value="Click Now" onClick='javascript:document.getElem entById("url").value = document.URL'> (later...) my $typed_url = param("url"); parse_url($typed_url)
Kristobaldude at Amazon Askville Visit the source
Other answers
MrItty, Thanks for your quick response! I actually fell ill the same night you responded, else I would have replied sooner. I appreciate your answer, and while it makes sense, I must say that I had actually tried that method earlier in the day to no avail. The problem I seem to be running into is that the param() method of the CGI object doesn't give me any results. I've tried to test various ways to see if my syntax was wrong, but it just isn't working. Moreso, the way all the literature, and forums, and the other developer in my office all say that (give $q is defined as new CGI) $q->param("whatever") should get the value of the form element on the page with the id of "whatever", but alas for some reason, (which has evadaded my investigations for over 3 days now), IT JUST DOESN'T WORK. I'm pretty much a novice poster and I'm not sure if there are ettiquette, protocol, or rules of style to follow, so forgive me (or better yet, let me know where I can find this style guide ;) if I inadvertently break one of them. Having said all that, I just spent about an hour filtering the core code out of my perl application, and got rid of all the frills an fluff. Below is the code I am using, in it's most basic form. The only thing that may look a little overwhelming is the chunk near the end where I was testing syntax. So, here is my code for your perusal: #================== CODE: =====================# #!/usr/bin/perl # # FILE: cgiAjaxTest3.pl # use strict; use warnings; # IMPORT MODULES use CGI qw(:standard); #use CGI::Carp qw(fatalsToBrowser); # Send error messages to browser use Data::Dumper; my $q = new CGI; my $html = qq~ <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.d td"> <HTML lang="en"> <HEAD> <TITLE>cgiAjaxTest4.pl</TITLE> </HEAD> <BODY> <form id="form1"> <input type="hidden" name="hidden1" value="I AM HIDDEN" id="hidden1" /> </form><BR /> <script type="text/javascript"> document.getElementById('hidden1').v alue = document.URL; </script> ~; # THIS IS WHERE I TESTED VARIOUS SYNTAXES ... my $htmlEnd .= '<hr><h2>$q->param():</h2>' ; $htmlEnd .= 'Dumper($q->param()): ' . Dumper($q->param()) . '<br />'; $htmlEnd .= '$q: ' . $q . '<br />'; $htmlEnd .= '$q->param: ' . $q->param . '<br />'; $htmlEnd .= '$q->param(): ' . $q->param() . '<br />'; $htmlEnd .= '$q->param("hidden1"): ' . $q->param("hidden1") . '<br />'; $htmlEnd .= '... end of param ...<br />'; $htmlEnd .= '</BODY></HTML>'; my $page = $q->header . $html . $htmlEnd; print $page; #================= END CODE: ==================# I have gone ahead and included the code, because I usually see people asked to post their code on other forums that I've looked at, so I figured I would save everyone the step of asking.... I hope that doesn't seem presumptuous. I really need a solution to this. I haven't found any answers on any of the blogs or forums or google searches and I'm really starting to get frustrated. My mind keeps saying "THERE'S NO WAY THAT THIS SHOULDN'T BE WORKING!! WTH???" Lol. Well thanks for your assistance so far and if you are able to understand where my code may be flawed or what I'm doing wrong, please let me know! Thanks in advance! -Kristobaldude
Kristobaldude
Here is a screenshot of my output in Chrome if that helps any.Thanks!
Kristobaldude
When you use CGI.pm, you have two options - functional style or object oriented style. You are combining both. When you import the ":standard" tag, you are telling CGI.pm you want to use the functional style. That means you don't declare a new $q object. You just use the various CGI.pm methods (param(), header(), etc) as pure function calls. So your options are either: 1) remove the ":standard" tag from your use CGI line, and make it just: use CGI; rather than use CGI qw(:standard); 2) Remove all instances of the $q variable. Remove the my $q = new CGI; line, and change all instances of $q->param() to simply param() Take a look at the "Programming Style" section of CGI.pm's documentation at http://search.cpan.org/dist/CGI/lib/CGI.pm#PROGRAMMING_STYLE for more information. Good luck!!
MrItty
The other thing (and far more important thing) I see wrong with your form is that you never submitted it. A form has to be submitted in order for the browser to receive the parameters. You never even created a submit button. That's your issue (the functional vs object oriented thing might also be a problem, but this is DEFINITELY a problem). Your web app needs to have two separate sections - one for if the page was just loaded, in which you create the form, and one for if the form was submitted, in which you display the results: use CGI; if (!param()) { #print form stuff, including the <input type=submit> button. #the javascript to set the hidden input's value goes into an onClick javascript method of this button } else { #call param() and display the results }
MrItty
@MrItty: Thanks for all your help! Yes, I discovered on my own after letting the code sit for a couple days what you correctly pointed out -- I never submitted the code! The next step is building the correct query string and sending back to browser. Thanks again! - Kristobaldude
Kristobaldude
Related Q & A:
- How can I change a value in an array?Best solution by Stack Overflow
- How do I remove a site from IIS7 using JavaScript?Best solution by Server Fault
- How can I pass global variables into a function?Best solution by Stack Overflow
- How can I convert a string number to a number in Perl?Best solution by Stack Overflow
- How do I get a value from a JObject?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.