View Full Version : Perl cuts off after <space>
Kickin
07-10-2005, 06:11 AM
I have a page that allows the user to log in and saves the name in a cookie. It then sends this along with all of the information in the form to a perl script which writes it to a file. The problem is if the user enters 'John Doe', perl writes 'John'. The rest of the information in the form is sent correctly, including data that has spaces.
The code in the html page looks like this:
<head>
var visitorName = prompt("Sign in with your name.", "")
SetCookie("User", visitorName, expDate, "/")
</head><body><form name="gate" action="" method="post">
<script language="JavaScript" type="text/javascript"><!--
document.write("<input type='hidden' name='user' value=",visitorName,">")
//-->
</script>
The code in the perl page looks like this:
#!/usr/local/bin/perl
use CGI ':standard';
my $user = param('user');
open (LOG, ">>../logs/sample.txt") || Error('open', 'file');
print LOG "user\n";
close (LOG);
Any thoughts on why this taking only the first name and not everything written? Thanks in advance for any insight.
mlseim
07-10-2005, 04:44 PM
Maybe this is a typo on your post ... but I noticed this:
#!/usr/local/bin/perl
use CGI ':standard';
my $user = param('user');
open (LOG, ">>../logs/sample.txt") || Error('open', 'file');
print LOG "$user\n";
close (LOG);
The failing part doesn't look like the Perl script ... it must be
in your HTML code with the Javascript and Cookies.
I'm not sure why it's necessary for you to use Javascript and cookies in your HTML ...
Here's an example where you use a normal <form> tag in your HTML and no Javascripting. The cookie(s) are set within your Perl script. The example will set two cookies, but you can only set one. Note ... you need to put YOUR DOMAIN name on the two DOMAIN lines. It then redirects to another page.
#!/usr/local/bin/perl
use CGI ':standard';
my $user = param('user');
open (LOG, ">>../logs/sample.txt") || Error('open', 'file');
print LOG "$user\n";
close (LOG);
$oreo = cookie( -NAME => 'User',
-VALUE => "$user",
-EXPIRES => '', # M for month, m for minute
-DOMAIN => '.mysite.com'); #<- Your domain name here.
$oreo2 = cookie( -NAME => 'Another_Value',
-VALUE => "$another_value",
-EXPIRES => '', # M for month, m for minute
-DOMAIN => '.mysite.com'); #<- Your domain name here.
$redirect = "http://www.mysite.com/thankyou.html";
print redirect( -URL => $redirect,
-COOKIE =>[$oreo,$oreo2]);
.
Kickin
07-10-2005, 08:19 PM
Thanks for your reply mlseim. The missing $ was just a typo when I pasted to the site, like you said. I have tried setting the cookie in the perl script and from the html, they both set fine, it just won't write to the file properly. I'm using js on the page to do form validation and such, but only posted the parts of code that seemed relevent. I set an alert in the html just before it sends to the perl and it shows both first and last name, but only writes the first name to the log. Any other ideas?
FishMonger
07-10-2005, 09:13 PM
Since you're setting "User" as a cookie, have you tried retrieving it as a cookie instead of a param?
#!/usr/local/bin/perl
use CGI;
my $q = new CGI;
my $user = $q->cookie('User');
open (LOG, ">>../logs/sample.txt") || Error('open', 'file');
print LOG "$user\n";
close (LOG);
mlseim
07-10-2005, 10:09 PM
Kickin ...
This might give you some clues.
I know that when you use a normal HTML form and have
a textbox with this value propery: value=Bill Smith
it will only send the first name.
But, if in quotes like this: value="Bill Smith" or value='Bill Smith'
it will send the whole thing.
Dig around with your Javascript parts and see if there's some
quotes missing where things are assigned to values or variables.
... just a thought.
Kickin
07-10-2005, 10:29 PM
Thank you both for your help. My knowledge of perl is extremely limited, just what I have picked up playing with it here and there. I changed it to read from the cookie and it works great. Thanks again.
vBulletin® v3.8.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.