By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,916 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,916 IT Pros & Developers. It's quick & easy.

Web form: Frustrating problem

P: 20
Hi everyone,

I'm writing a web form and a backend Perl script to send a query to the administrator.

V1 of the script is working well, it's basically just a simple get-data-send-email [sendmail] script. V2, on the other hand, is a nightmare. I added just a few changes.

In the HTML page (which has become an SHTML page to support a server includes:

Expand|Select|Wrap|Line Numbers
  1. <form method="post" onsubmit="return checkform()" action="/cgi-bin/activate_v2.pl" >
  2.  
  3. <input type="hidden" id="ip" value="127.0.0.1" />
  4.  
In the javascript:

Expand|Select|Wrap|Line Numbers
  1. /* New code: Gets IP address using SSI */
  2. var ip = '<!--#echo var="REMOTE_ADDR"-->';
  3.  
  4. /* New code: Adds IP address to hidden field */
  5. document.getElementById("ip").value = ip;
  6.  
In the Perl:

Expand|Select|Wrap|Line Numbers
  1. my $ip = "127.0.0.1";
  2.  
  3. $ip = param('ip');
  4.  
  5. # now we can check the data
  6.  
  7. if ($fullname eq "" || $address eq "" || $username eq "" || $sender eq "") {
  8.  
  9.     # we have incomplete data, so we will serve an error
  10.  
  11.     # set the web page parameters
  12.  
  13.     $content_header = 'Incomplete Information';
  14.     $content = '<p>Your activation request not been submitted.</p> <p>You did not fill out all of the requested information on the previous page. Please return to the main activation page and complete the form as directed.</p> <p>If you feel that you received this message in error, please <a href="mailto:admin@our-website.com">email the web services administrator</a>.</p>';
  15.     $site_url = '/activate/';
  16.     $link = 'Click here to return to the previous page';
  17.  
  18.     # $activate = 0;  # not needed as this parameter was set at the beginning
  19.  
  20. }
  21.  
  22. else {
  23.  
  24.     # everything is okay
  25.     $activate = 1;
  26.  
  27. if ($activate == 1) {
  28.  
  29.     # it's okay to submit the request
  30.  
  31.  
I'm completely lost (as evidenced by my previous, late unlamented post...).

I should mention that I have tried changing the POST method to GET to try and see what is passed to the script, but nothing seems to be being passed over.

Also, I originally broke the code up into subroutines, but when the program didn't work I lumped it all back together again for troubleshooting purposes.

Perhaps I should break this problem into two and try to run the Perl script from the shell? How can I pass parameters to the script (i.e. "ip", "fullname" etc.)?
Mar 28 '07 #1
Share this Question
Share on Google+
11 Replies


P: 20
I should mention the actual problem I guess: I always get the error page, not the success page. When I've tried printing the variables (ip, fullname etc) collected at the beginning of the script to the screen (the HTML page), they display as blank.
Mar 28 '07 #2

KevinADC
Expert 2.5K+
P: 4,059
one of these conditions is true so your script does the error stuff:

Expand|Select|Wrap|Line Numbers
  1. if ($fullname eq "" || $address eq "" || $username eq "" || $sender eq "") {
but there is no way to tell by looking at the code or from anything you said to know why that is. Are the above variables getting defined somewhere? Print them out to the screen to see what their values are.
Mar 28 '07 #3

P: 20
Indeed, unfortunately all of them are true. The problem is I don't know why.
Mar 30 '07 #4

KevinADC
Expert 2.5K+
P: 4,059
The obvious question is: where are these variables being defined?

Expand|Select|Wrap|Line Numbers
  1. if ($fullname eq "" || $address eq "" || $username eq "" || $sender eq "") {
are they data from a form? Where is the data from the form getting put into those variables?
Mar 30 '07 #5

P: 20
On the form:

Expand|Select|Wrap|Line Numbers
  1. <form method="post" onsubmit="return checkform()" action="/cgi-bin/activate_v2.pl" >
  2.  
  3. <input type="hidden" id="ip" value="127.0.0.1" />
  4.  
  5. <p><table>
  6. <tr>
  7.     <td>Full Name:</td>
  8.     <td><input type="text" id="fullname" size="31" /></td>
  9. </tr>
  10. <tr>
  11.     <td>Street Address:</td>
  12.     <td><input type="text" id="address" size="31" /></td>
  13. </tr>
  14. <tr>
  15.     <td>Email Address:</td>
  16.     <td><input type="text" id="email" size="31" /></td>
  17. </tr>
  18. <tr>
  19.     <td>Website User Name: </td>
  20.     <td><input type="text" id="username" size="31" /></td>
  21. </tr>
  22. </table></p>
  23.  
  24. <p><button type="submit">Submit</button></p>
  25.  
  26. </form>
  27.  
There's also a smigden of Javascript to alter the 'ip' hidden field, which I have noted in my first post.

From the Perl script:

Expand|Select|Wrap|Line Numbers
  1. my $sender = 'noreply@our-website.com';
  2. my $fullname = '';
  3. my $address = '';
  4. my $username = '';
  5. my $ip = "127.0.0.1";
  6.  
  7. ### some other stuff, not posted ###
  8.  
  9. # gets data from the initial HTML form
  10.  
  11. # get all of the form contents
  12.  
  13. my $formcontents = "";
  14.  
  15. foreach my $field (param) {
  16.  
  17.     foreach my $value (param($field)) {
  18.  
  19.         $formcontents.= "$field: $value\n";
  20.  
  21.     }
  22. }
  23.  
  24. # now let's get the values from the form
  25.  
  26. $fullname = param('fullname');
  27.  
  28. $address = param('address');
  29.  
  30. $sender = param('email');  # no need for a my statement
  31.  
  32. $username = param('username');
  33.  
  34. $ip = param('ip');
  35.  
Mar 31 '07 #6

KevinADC
Expert 2.5K+
P: 4,059
hard to tell, it's like trying to diagnose a patient when all you can see is an arm or a leg. Assuming your script has loaded the CGI module and the form fields are filled in, I can't see why the script doesn't seem to work right.
Mar 31 '07 #7

P: 20
The problem is almost certainly in the changes I quoted at the beginning, V1 has none of these issues.

But for the life of me I am stuck, and my teacher is as well. Looks like the script needs to be scrapped and I will have to do without the server-side error checks.

Thanks.
David
Mar 31 '07 #8

KevinADC
Expert 2.5K+
P: 4,059
Try removing this part of the code

Expand|Select|Wrap|Line Numbers
  1. foreach my $field (param) {
  2.  
  3.     foreach my $value (param($field)) {
  4.  
  5.         $formcontents.= "$field: $value\n";
  6.  
  7.     }
  8. }
it might be emptying the buffer so subsequent attempts to get data out of param() might be empty.
Mar 31 '07 #9

P: 20
Sorry about the delay in posting -- exams.

Great idea, but this didn't work. Thanks for the suggestion, any more? (hoping against hope here)

If you're ever in Australia look me up and I'll buy you a beer, heh... you deserve it, mate :-)
Apr 4 '07 #10

KevinADC
Expert 2.5K+
P: 4,059
print $formcontents out to a file or the screen and report back what it's value is.


Expand|Select|Wrap|Line Numbers
  1. foreach my $field (param) {
  2.  
  3.     foreach my $value (param($field)) {
  4.  
  5.         $formcontents.= "$field: $value\n";
  6.  
  7.     }
  8. }
Apr 4 '07 #11

P: 20
$formcontents is blank:

Form Contents:
There is literally no content, specifically no undef message. Just before the code you quoted, I defined $formcontents as "".

Thank you for your continued help with this.

David
Apr 6 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.