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

Hello I have a webpage with an input section that then runs a perl script with the variables from the input section from the page and then returns the results to the same page this is the code for the HTML page - When it returns the results they are not formatted (with new lines or tabs correctly)
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquer +y.min.js"></script> <script> $(document).ready(function() { $('input').keyup(function() { var empty = false; $('input').each(function() { if ($(this).val().length == 0) { empty = true; } }); if (empty) { $('.actions input').attr('disabled', 'disabled'); } else { $('.actions input').attr('disabled', false); } }); $("#btnSearch").click(function(event) { var term = $("#urlField").val(); perlExecute(term); }); function perlExecute(term){ $.ajax({ type: 'POST', url: '/cgi-bin/domain-.pl', data: { 's': term }, success: function(res) { $("#result").text(res); }, error: function() {$("#result").text("Search Failed\nPossible +reasons:-\nThere illegal characters in your search.)");} }); }; }); </script> </head> <body> <div class='form'> <input type="text" name="s" placeholder="Search..." id="urlField +"> <div class='actions'> <input type="button" value="Search" id="btnSearch" disabled="dis +abled" > </div> </div> <div id="result"></div> <p id="content">Lorem Ipsum</p> </body> </html>
The error code if the script fails to run is return in the webpage like this
Search Failed Possible reasons:- There illegal characters in your sear +ch
With no recognition of the my new-line characters or the other extreme is
["Search Failed\nPossible reasons:-\nThere illegal characters in your +search."]
With my new-line characters printed in the text. - how can I format the error text the way i want it to appear? The other issue is that the command being run is
#!/usr/bin/perl -w use warnings; use JSON; use CGI; my $cgi = CGI->new(); my $string = $cgi->param('s'); $ns_lookup = `dig \@4.2.2.2 +norecurse +nocmd +noquestion +nostats +no +comments +authority +noadditional $domain NS`; chomp($ns_lookup); print "Content-type: text/html\n\n"; print $ns_lookup;
The above code prints out the following
google.co.zw. 59751 IN NS ns3.google.com. google.co.zw. 59751 IN NS ns +4.google.com. google.co.zw. 59751 IN NS ns1.google.com. google.co.zw. + 59751 IN NS ns2.google.com.
Instead of
google.co.zw. 59751 IN NS ns3.google.com. google.co.zw. + 59751 IN NS ns4.google.com. google.co.zw. 59751 IN + NS ns1.google.com. google.co.zw. 59751 IN NS ns2.goog +le.com.
how can i format the text and make it more legible.

Replies are listed 'Best First'.
Re: Format script output to jQuery ajax call
by Your Mother (Archbishop) on Mar 11, 2016 at 13:12 UTC

    All right, I read your OP this time. :P Your original code is not quite right. You've probably adjusted it to work on your box but what you posted is both broken and dangerous if not fixed properly. Always use strict.

    You get $string from the CGI parameters but then use $domain in the dig call. You also pass user input directly to the shell with the backticks (``). This is about as serious a security mistake as there is. Also, there is no JSON in your code or implied in your jQuery code, so drop the use statement.

    Adjusted, and simplified a little for ease of testing, with non-OO CGI and shell quoting capture of the command. You will probably have to install Capture::Tiny but it may be the easiest way to make the call to dig safe from exploits.

    #!/usr/bin/env perl use strict; use warnings; use CGI ":standard"; use Capture::Tiny "capture_merged"; my $domain = param("s") || "example.org"; my $dig = capture_merged { system "dig", $domain }; print header(), escapeHTML($dig);

    Your display problem has two possible solutions. Either fix the style of the #result div or jQ =~ s/text/html/

    <div id="result" style="white-space:pre"></div>
    # CGI change print header(), pre(escapeHTML($dig)); # plus JS change $("#result").html(res);
Re: Format script out in JSON for webpage
by NetWallah (Canon) on Mar 10, 2016 at 22:07 UTC
    Add <pre> tags to your returned response:
    print "<pre>$ns_lookup\n</pre>";
    or (easier to read):
    print $cgi->pre("$ns_lookup\n");

            This is not an optical illusion, it just looks like one.

      On this note, please everyone remember, unescaped HTML is on the keyring to your kingdom. Never ever, ever, ever, ever write a song about the Sibbie. Or forget to escape HTML.

      print $q->pre( $q->escapeHTML( $ns_lookup ) );
        Hi This does not seem to have worked the output is now
        <pre>google.co.zw. 21014 IN NS ns1.google.com. google.co.zw. 21014 IN +NS ns2.google.com. google.co.zw. 21014 IN NS ns3.google.com. google.c +o.zw. 21014 IN NS ns4.google.com.</pre>