For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > June 2006 > help with 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 help with a script
Squalle

2006-06-30, 3:55 am

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.

Rick


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com