Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

perl cgi server response issue or not?

by heatblazer (Scribe)
on Jun 09, 2012 at 16:28 UTC ( #975314=perlquestion: print w/ replies, xml ) Need Help??
heatblazer has asked for the wisdom of the Perl Monks concerning the following question:

Hello all

I am trying some really basic server script for outputing a html page. Firefox firebug says it`s OK but I don`t see a thing. Can you look at my code?

here is my html form:

<!DOCTYPE html> <html> <head><title>PERL TEST</title> <script> window.onload = function() { var pressIt = document.getElementById('submit'); pressIt.onclick = populatePage; }; function populatePage() { var req = newRequest(); var url = "http://localhost/cgi-bin/response.pl"; var name = document.getElementById("name").value; var age = document.getElementById('age').value; console.log(name, age); if ( name && age && req ) { url += ("?name=" + name + "&age="+age); req.open("GET", url ); req.send(url); } } function newRequest() { try { var request = new XMLHttpRequest(); } catch ( otherMS ) { try { request = new ActiveXObject("Msxml2.XMLHTTP"); } catch ( otherMS ) { try { request = new ActiveXObject("Microsoft.XML +HTTP"); } catch ( failed ) { request = null; } } } return request; } </script> </head> <body bgcolor="#ccffcc"> <h1>Please fill the small form below</h1> <form> <input type='text' value='' id='name' name='name' >Enter name</inp +ut> <input type='text' value='' id='age' name='age' >Enter age</input> <input type='button' value='Submit' id='submit' > </form> </body> </html>

And Perl`s response form cgi-bin:

#!/opl/lampp/bin/perl use strict; use CGI; ## RESPONSE TO XMLHTTP: my $response = CGI->new(); my ( $name, $age ) = ($response->param("name"), $response->param("age" +) ); if ( $name && $age ) { print "Content-type: text/html\n\n"; print "<html><head></head>"; print "<body><h1>Hello $name </h1></body>"; print "</html>"; } else { exit(0); }

Don`t mind the example, I see in the debug that server responded OK, but I don`t see the actual HTML page...

Comment on perl cgi server response issue or not?
Select or Download Code
Re: perl cgi server response issue or not?
by aaron_baugher (Deacon) on Jun 09, 2012 at 16:52 UTC

    I'm not sure why you need all that Javascript to submit a form, something that your browser would do just fine on its own. But to the debugging question, if your script isn't executing the print statements, then it seems that your if condition isn't returning true. Try adding a print statement to your else clause, and see if that prints. If it does, try printing $name and $age and see if the problem is with one or both of them. Another aid would be to use Data::Dumper or one of its sisters to dump the contents of $response, and see what's really in there.

    This is basic debugging. When your program doesn't give you the output you expect, have it tell you what it's doing, what parts of the program are executed, what values it has in different places. There are more sophisticated debugging tools, but I've found a lot of problems just by adding a   print 'got here';   line in different spots and reloading.

    Aaron B.
    Available for small or large Perl jobs; see my home node.

      Well I am advancing into JSONP and I want to do some testings before it. I know the form tag itself has it`s submit options but I still need that code I`ve done and it is going even more complicated...

        Fair enough. In that case, you may want to look into something like jQuery, which makes the Ajax/JSON stuff a lot simpler, and invisibly handles the browser incompatibilities that require that mess of XMLHttp/ActiveX stuff in plain Javascript.

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Re: perl cgi server response issue or not?
by Anonymous Monk on Jun 10, 2012 at 07:31 UTC

    Don`t mind the example, I see in the debug that server responded OK, but I don`t see the actual HTML page...

    Great, now you know to do your debugging in the browser which is responsible for displaying html -- it can also (probably) tell you why its not displaying it

      Well, good point but no, everything seems to be OK, the only problem is that perl`s script output does not display into the browser. That is strange since I`ve tested direct form validation without JS and it is working ok... But here it`s fishy...

        Well, good point but no, everything seems to be OK, the only problem is that perl`s script output does not display into the browser. That is strange since I`ve tested direct form validation without JS and it is working ok... But here it`s fishy...

        Maybe you should figure out why? something something http headers

        When you call the open() and send() methods on your URL, that does the request, but you never do anything with the data your server returns. Look into the properties responseText and responseXML to get at your data. You'll also need to use the onreadystatechange event to know when the data is ready for use.

        Or you could replace the entire Javascript portion with a few lines of jQuery. It takes a lot less typing to get and change the values of elements, and the ajax routines (here using get) provide callbacks that will be fired when the data is ready (or under other conditions you may want to catch). You also don't have to construct a query string for your URL, and worry about what happens if people put unexpected characters in the fields.

        $(document).ready(function() { $("#submit").click(function(e) { e.preventDefault(); // don't let browser submit var name = $('#name').val(); var age = $('#age' ).val(); if( name && age ){ $.get(url, { name: name, age: age }, function(data){ $('#somediv').html(data); // replace div contents with new + data }); } else { alert('Missing form fields!); } }); });

        Aaron B.
        Available for small or large Perl jobs; see my home node.

Log In?
Username:
Password:

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

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

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





    Results (184 votes), past polls