Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

progress bar

by bigup401 (Pilgrim)
on Apr 09, 2019 at 21:01 UTC ( #1232371=perlquestion: print w/replies, xml ) Need Help??

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

i really need help on my script to make progressbar to work. progressb +ar in cgi web development. its the one which has given me hard time t +o figure out. every time i try it i get bad results. i need to have a + working example to practice it more and more #!/usr/bin/perl -w use lib '.'; use strict; use warnings; use DBI; use CGI; use MIME::Lite; use CGI::Carp qw(fatalsToBrowser); my $q = new CGI(); my $host = "localhost"; my $usr = "root"; my $pwd = ""; my $dbname = "tbl_users"; my $dbh = DBI->connect("DBI:mysql:$dbname:$host", $usr, $pwd, { AutoCommit => 0, RaiseError => 1, }) or die $DBI::errstr; my $sub = $q->param("sub"); my $msg = $q->param("msg"); my $success; if ($sub) { my $getemails = $dbh->prepare("SELECT DISTINCT EMAIL FROM USERS"); $getemails->execute(); my $dbemails = $getemails->fetchall_arrayref; my $emails = join ',',map{$_->[0]}@$dbemails; my $to = ''; my $from = 'noreplay@site.com'; my $subject = $sub; my $message = " $msg "; my $MSG = MIME::Lite->new( From => $from, To => $to, Bcc => $emails, Subject => $subject, Data => $message ); $MSG->send; $success = "mail was sent successfully"; } $dbh->disconnect || die "$DBI::errstr\n"; print "Content-type: text/html\n\n"; print <<START_HTML; <!DOCTYPE html> <html> <head> <title>Mail Send</title> <style type="text/css"> body { background-color: white; font-family: Verdana; font-size: small; color: black } #trough { background-color: silver; border: 1px solid black; height: 24px } #bar { background-color: #669900; height: 24px; width: 1% } </style> </head> <body> <h1>Mail My DB Customers</h1> <div id="form" style="display: block; margin: auto; width: 350px"> <fieldset> <legend>Send Mail</legend> <form name="mail" method="post" onSubmit="return sendMail(thi +s)"> Subject: <input type="text" name="sub" size="20"><br> Message: <input type="text" name="msg" size="20"><br> <input type="submit" value="Send Mail"> </form> </fieldset> </div> <div id="progress" style="margin: auto; width: 350px"> <fieldset> <legend>Sending...</legend> <div id="trough"><div id="bar"></div></div> Percent: <span id="percent">0</span>% </fieldset> </div> <span>$success</span> <div id="debug"></div> <script type="text/javascript"> // When the form is submitted. function sendMail(frm) { // Show the progress indicator. document.getElementById("progress").style.display = "block"; // Wait a bit and make ajax requests. setTimeout("getProgress()", 1000); return true; } // Poll for our progress. function getProgress() { var ajax = new XMLHttpRequest(); ajax.onreadystatechange = function() { if (ajax.readyState == 4) { gotProgress(ajax.responseText); } }; ajax.open("GET", "upload.pl" + Math.floor(Math.random()*99999) +, true); ajax.send(null); } // Got an update function gotProgress(txt) { // Get vars outta it. var percent = 0; // Was it an error? if (stat[0] == "error") { document.getElementById("debug").innerHTML += "error: " + +stat[1]; setTimeout("getProgress()", 1000); return false; } // Separate the vars. var parts = stat[1].split("&"); for (var i = 0; i < parts.length; i++) { var halves = parts[i].split("="); if (halves[0] == "percent") { percent = halves[1]; } } document.getElementById("debug").innerHTML += "percent:" + per +cent + "<br>\n"; // Update the display. document.getElementById("bar").style.width = parseInt(percent) + + "%"; document.getElementById("percent").innerHTML = percent; // Set another update. setTimeout("getProgress()", 1000); return true; } </script> </body> </html> START_HTML

Replies are listed 'Best First'.
Re: progress bar
by Corion (Pope) on Apr 10, 2019 at 07:36 UTC

    The general concept is outlined very well in Watching long-running processes through CGI.

    My suggestion is that you read this article and then implement it.

    As you don't tell us where exactly you have problems, my suggestion is to first eliminate all the Javascript from your approach and implement the solution in the linked post. After you got that working, you can easily add Javascript again to update parts of your page instead of reloading the complete page.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: progress bar
by LanX (Cardinal) on Apr 10, 2019 at 10:53 UTC
      That's a follow up to send mail ?

      Most likely, yes. bigup401 has a long trail of abusing perlmonks as a free (as in beer) code writing service. To me, it looks like a single job rotating around a multi-language web application that needs to mail some stuff and use the Paypal API, accepted by someone who has not the slightest idea of how all of that could ever work. bigup401 has proven several times to be unwilling to learn, unwilling to answer questions, unwilling to show a minimum of efford when writing postings. For me, it's just a waste of time to answer postings of bigup401. That time is better spend answering questions of people showing at least a tiny bit of effort, and are willing to learn just a tiny bit.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        > For me, it's just a waste of time to answer postings of bigup401.

        That's why my reply consists of 8 words only ;-P

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2020-07-12 09:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?