Home > Archive > PERL CGI Beginners > August 2006 > missing info in a script
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
missing info in a script
|
|
| Squalle 2006-08-17, 6:55 pm |
| Hello,
I have a form that, using javascript, allows a user to add form controls as
needed. The form code is:
--------------------------------------------------------------------------------
<html>
<head>
<title></title>
<script type="text/javascript">
var arrInput = new Array(0);
var arrInputValue = new Array(0);
function addInput() {
//arrInput.push(createInput(arrInput.length));
arrInput.push(arrInput.length);
//arrInputValue.push(arrInputValue.length);
arrInputValue.push("");
display();
}
function display() {
document.getElementById('parah').innerHTML="";
for (intI=0;intI<arrInput.length;intI++) {
document.getElementById('parah').innerHTML+=createInput(arrInput[intI],
arrInputValue[intI]);
}
}
function saveValue(intId,strValue) {
arrInputValue[intId]=strValue;
}
function createInput(id,value) {
return "<input type=\"text\" name=\"details"+ id +"\" id=\"test"+ id +"\"
onChange=\"java script:saveValue("+ id +",this.value)\" value=\""+ value
+"\"><br>";
}
function deleteInput() {
if (arrInput.length > 0) {
arrInput.pop();
arrInputValue.pop();
}
display();
}
</script>
</head>
<body>
<form action="test.cgi" method="post">
Number: <input type="text" name="number" size="6" maxlength="6">
<a href="java script:addInput()">Add field(s)</a> | <a href="java script:deleteInput()">Remove field(s)</a>
<p id="parah"></p>
<input type="submit" value="Add Listing"> <input type="reset" value="Clear">
</form>
</body>
</html>
--------------------------------------------------------------------------------
Each time the user adds a text field, the javascript assigns each field the
name "detailsX" where X is a number. I'm trying to make the "added" text
fields write to a small flatfile database (<20 items) where each added text
field is a new line and the file name would be what the user enters in the
"number" form field. My script looks like this:
--------------------------------------------------------------------------------
#!/usr/bin/perl
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/~!/ ~!/g; $FORM{$name} = $value;
}
$number = $FORM{'number'};
$details[id] = $FORM{'details[id]'};
open(STUFF,">>$number.db");
foreach ($details[id]) {
print STUFF "$details[id]\n";
}
close(STUFF);
print "Content-type: text/html\n\n";
print "Success!\n";
--------------------------------------------------------------------------------
I'm just not sure how to tell the script what the 'id' is for each text
field. Any ideas?
Thanks.
| |
| Paul Lalli 2006-08-17, 6:55 pm |
| Squalle wrote:
> I have a form that, using javascript, allows a user to add form controls as
> needed.
>
>
>
> Each time the user adds a text field, the javascript assigns each field the
> name "detailsX" where X is a number.
Don't. Just let each one be 'details'.
> I'm trying to make the "added" text
> fields write to a small flatfile database (<20 items) where each added text
> field is a new line and the file name would be what the user enters in the
> "number" form field. My script looks like this:
> --------------------------------------------------------------------------------
>
>
> #!/usr/bin/perl
Add the lines:
use strict;
use warnings;
use CGI::Carp qw/fatalsToBrowser/;
> read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
> @pairs = split(/&/, $buffer);
> foreach $pair (@pairs) {
> ($name, $value) = split(/=/, $pair);
> $value =~ tr/+/ /;
> $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
> $value =~ s/~!/ ~!/g; $FORM{$name} = $value;
> }
Replace ALL of this with:
use CGI qw/:standard/;
>
> $number = $FORM{'number'};
my $number = param('number');
> $details[id] = $FORM{'details[id]'};
my @details = param('details');
> open(STUFF,">>$number.db");
1) use the three argument form of open
2) use lexical filehandles, not global barewords
3) check the return value of all system calls
open my $STUFF, '>>', "$number.db" or die "Cannot open $number.db:
$!\n";
> foreach ($details[id]) {
> print STUFF "$details[id]\n";
> }
foreach my $detail (@details) {
print $STUFF "$detail\n";
}
> close(STUFF);
close $STUFF or die "Cannot close $number.db: $!\n";
> print "Content-type: text/html\n\n";
print header();
> print "Success!\n";
print start_html('Success!'), h1('Success'), end_html();
Paul Lalli
|
|
|
|
|