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

Very close to done with my term project I have been working on/stressing about for days, I just have one small error. My html isn't sending over the values for name and day, and it must be html because when I assign default values to the parameters it sends those to the database. Heres my code. HTML
<html> <center><h1><div>Enter the user that you would like to change data for + .</div></h1></center> <form action = "http://127.0.0.1/cgi-bin/change.cgi" method="POST"> <div>Name: <input name="name" size="15"></div> <div>New phone number: <input name="phone" size="15"></div> <div>New on-call day: <input name="day" size="15"></div> <div><input type="submit"></div> </form> </html>
CGI
#!/usr/bin/perl -w use CGI qw(standard); use DBI(); use DBD::mysql; use warnings; use diagnostics; my $q = CGI->new; my $name = $q->param('name'); my $number = $q->param('phone'); my $day = $q->param('day'); my $dbh = DBI->connect('dbi:mysql:oncall','webuser','password') or die "Connection Error: $DBI::errstr\n"; my $sth=$dbh->prepare("UPDATE users SET day = ?, phone =? WHERE na +me = ?"); $sth->execute($day,$number,$name); #Disconnecting from database $dbh->disconnect(); print $q->header, $q->h1('you submitted the change!'), $q->end_html;

Replies are listed 'Best First'.
Re: Almost done with project.
by marto (Cardinal) on Jul 18, 2013 at 13:51 UTC

    Is that really your HTML? No body tag, no title, no input type specified? I'd sort those things our and try again. Regarding your Perl code, you're using placeholders which is good, but you may want to consider the following:

    • -w and use warnings;? Go with one and not the other, there's no need for both.
    • Consider adding use strict;

    Actually, this is as far as I got before reading the your previous threads. The code here differs in some ways, you've removed use strict;. Also people have provided good advice for debugging CGI scripts which are applicable here. I suggest you work your way through your previous responses. Ignoring this advice and posting again isn't going to help you learn how to do things.

Re: Almost done with project.
by rjt (Curate) on Jul 18, 2013 at 13:57 UTC

    Your HTML is fine not the cause of your current problem, and I don't see anything wrong with your script, either. Have you checked the browser error log? What gets returned to the browser when you submit the form?

    I tried this with your HTML:

    #!/usr/bin/perl use CGI ':standard'; use CGI::Carp 'fatalsToBrowser'; use warnings; my $q = CGI->new; print $q->header; $q->h1('you submitted the change!'); print $q->p("$_ = " . CGI::escapeHTML($q->param($_) // '-?-')) for qw< +name phone day>; print $q->end_html;

    I get whatever I submit echoed back, as expected. Note the use of CGI::Carp to assist in diagnosis.

      "Your HTML is fine...."

      Not really, unless -- a la Bill Clinton -- what you mean depends on how you define "fine." It's not "fine" by the specs because, as noted above, it's lacking both head and body tags (and their closes) to satisfy the w3c "loose" standard and ... and it's not even close to "strict" ...nor to good practice.

      If you mean "is not the cause of your problem," it would be better to say that, than to make a statement which -- strictly or loosely -- does not comport with the facts.

      Even this version of OP's code is at least one short of a dozen in terms of compliance with w3c -- tho that ommission and some of the edits are inconsequential to OP's problem.

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http:/ +/www.w3.org/TR/html4/loose.dtd"> <html lang="en"> <head> <title>"More-or-less 'fine' (eg: 'compliant') html"</title> </head> <body> <center> <!-- deprecated in favor of style sheets or even in-l +ine style --> <h1>Enter the user for whom you would like to change data.</h1> </center> <form action="http://127.0.0.1/cgi-bin/change.cgi" method="POST"> <!-- + see below --> <p> <label for="fullname">Change Full Name to: </label> <input name="fullname" id="fullname" size="15"><br> <!-- title ta +g omitted --> <label for="phone">New Phone Number: </label> <!-- blanks lines +, indents --> <input name="phone" id="phone" size="15"><br> <!-- for legibili +ty only --> <label for="day">New on-call day: </label> <input name="day" id="day" size="15"><br> <input type="submit"> # divs without attributes are often useful, +but a </p> # para with breaks is (at least arguably) mo +re # correct semantically and renders in essnti +ally # the same way. </form> </body> </html> <!-- NO spaces around any equals sign unless quoted for rendering -->
      If I've misconstrued your answer, apologies to all the electrons which were inconvenienced by the creation of this post.
        If you mean "is not the cause of your problem," it would be better to say that, than to make a statement which -- strictly or loosely -- does not comport with the facts.

        Agreed. I was imprecise. You intuited my meaning correctly; thanks for clarifying.

      It's giving me a UTF-8 error of some sort, stating it was submitted in windows 1252 encoding.

        Error messages, cryptic as they may seem, are often fairly precise in what they state. When you say "a UTF-8 error of some sort", it means you gave up without reading and trying to decipher the "what" and "why" of the message. I suggest posting the actual message rather than an imprecise summary of it.


        Dave

Re: Almost done with project.
by daxim (Curate) on Jul 18, 2013 at 13:54 UTC
    I cannot reproduce the problem. For me, the form works as expected:
    POST /cgi-bin/change.cgi HTTP/1.1
    Host: 127.0.0.1:80
    Content-Type: application/x-www-form-urlencoded
    
    name=1&phone=2&day=3