ianshortreed has asked for the wisdom of the Perl Monks concerning the following question:

I am working on a simple dictionary look-up app which passes a search term and the results are returned to a hmtl page. I would like to return the results to the same page rather than a new page using a <div>search_results</div> section.
Is there a quick and dirty way to do this?
The page in question is here:
http://www.mercury-soft.com/Passport-i

Thanks in advance for any help.
  • Comment on Printing html search results to the same page <div>

Replies are listed 'Best First'.
Re: Printing html search results to the same page <div>
by ikegami (Patriarch) on Apr 13, 2008 at 05:22 UTC
    You could use JavaScript to fetch the results (as in AJAX) and insert the received result into the DIV.
Re: Printing html search results to the same page <div>
by tachyon-II (Chaplain) on Apr 13, 2008 at 07:34 UTC

    This is not a perl question it is a javascript question. Anyway here is how to do it using the XMLHTTP object. It is pretty easy. All we do is link our submit button to a js function. This function returns false (as with input validation when there is an error) so the form does nothing when it returns. The function groks the search string, assembles the GET request, sends it, and dumps the data into the div. Jobs done.

    As a side note the search CGI needs work. If you search for 'a' it will dump 40,000+ results into the browser, if you type 'b'....and by the time you get to 'z' the entire DB has been extracted. If you type '*' it will tell you why. You probably don't want that to be happening.

    <html> <head> <script type="text/javascript"> var xmlhttp; // need this global to deal with bug issue function loadXMLDoc(url,responseHandler,async) { xmlhttp = null; if (window.XMLHttpRequest) { // code for Firefox, Opera, IE7 xmlhttp = new XMLHttpRequest(); } else if (window.ActiveXObject) { // code for IE6, IE5 xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp != null) { xmlhttp.onreadystatechange = responseHandler; xmlhttp.open("GET",url,async); xmlhttp.send(null); } else { alert("Your browser does not support XMLHTTP."); } } function updateSearch() { if (xmlhttp.readyState == 4) { if (xmlhttp.status == 200) { document.getElementById('results').innerHTML = xmlhttp.res +ponseText; xmlhttp.close; } else { alert("Problem retrieving data:" + xmlhttp.statusText); } } } function doSearch() { var search = escape(document.searchform.Search_Term.value); var url = "http://cgi.mercury-soft.com/cgi/mercury-soft.com/user-c +gi-bin/passport/passport.cgi"; url = url + "?Formaction=Searchforword&Search_Term="+search+"&B=Ye +s&x=1&y=1"; loadXMLDoc(url,updateSearch,false); return false; } </script> </head> <form name="searchform"> <input type="text" name="Search_Term"> <input type="button" value="Submit Form" onClick="doSearch()"> </form> <div id="results"> No results. </div> </body> </html>
      Many thanks for your suggestions!
      Ifs a javascript question, don't answer it :)

        Why not?

        Does it disturb your sense of Perl purity in some way? If so feel free to login and upvote ikegami and downvote me.

        Many of the questions on PM relate somewhat indirectly to perl and deal with server issues, compilation issues, OS issues, etc. PM would be a sad place if all the OP in such cases could hope for was a "this is not a Perl question....".

        Why is that a javascript question?
        I don't see why this couldn't be done with a (perl) cgi that displays search results if there is no search-term, and only the search dialog otherwise

        Part of what I love so much about this place is that you can get advice which in tangential to Perl. If you couldn't then a bookshelf of Perl books would be roughly equal in value. Since you can get architecture, compiler, platform, interface, glued-languages, DB, templates, even the odd CSS and such, almost all the advice top notch, this place is unbeatable; and Perl-like in its omnivorous nature.

Re: Printing html search results to the same page <div>
by oko1 (Deacon) on Apr 13, 2008 at 13:20 UTC

    If I interpret "same page" to mean "page that looks like this one" rather than "current page without even reloading", the the answer is pretty simple; in fact the documentation for the CGI module shows pretty much this approach.

    #!/usr/bin/perl -wT use strict; use CGI qw/:standard/; $|++; print header, start_html; ### ### Generate your page - everything up to the <div> you want - here ### if (my $search = param('search')){ ### ### Retrieve the results based on the search term ### print div("search_results go here"); } ### ### The content you want after the search <div> goes here ### print end_html;

    Otherwise, ikegami and the others are right - it's a JS question.

    
    -- 
    Human history becomes more and more a race between education and catastrophe. -- HG Wells
    
      This is exactly what I was looking for. I wanted to stay in Perl to make things uniform which is why I posted here! Cheers!