0

Edited** Being a Perl newbie is tough lol. I just used regex and my hash I read my cgi paramaters into.

my $regex = qr/tierrate/;
my $count = 0;

foreach (grep { /$regex/ } keys %form) {
$count++;
}

Then I can just use that count to determine my insert loop.


once again, I'm new to programming. My Jquery and Perl skills are beginner at best.

I'm looking to be able to insert multiple records into a database table based on the amount of table rows added by the user. I'm not really sure how to handle this as this is my first nonstatic attempt. I'm using cgi.pm and dbi.pm.

I'm displaying some HTML based on a button click. I'm displaying and trying to insert "tier rate" "tier range minimum" and " tier range maximum" for each time the user selects "add tier" button. The user can click the button as many times as needed and they will get an additional table row for each click, I'm not sure how to loop through a dynamic amount of rows.

Based on the user clicking the "add tier" button, I display HTML.

\$('#addnewtier').click(function() {

\$('<tr id="inctier'+ (raisetierid++) +'"><TD ALIGN=left><font color="#ADD8E6">Tier '+ (rateplus++) +' Rate</TD><TD ALIGN=left><input type="text" name="tierrate'+ (tierrate++) +'" size=10 value="00.00" ></TD><TD ALIGN=left><font color="#ADD8E6">Tier '+ (rangeplus++) +' Range</TD><TD ALIGN=left><font color="#ADD8E6">Minimum&nbsp;<input type="text" name="rangemin'+ (rangemin++) +'" size=10 value="" ><font color="#ADD8E6">&nbsp;Maximum&nbsp;<input type="text" name="rangemax'+ (rangemax++) +'" size=10 value="" ></TD>').appendTo("#inctier1");

})

The user can click the button as many times as they wish. I'm familiar with being able to grab the input on a set number of something, but not so much with a dynamic number of something. I am declaring the variables I'm incrementing just above the code I've posted. Thanks in advance for the help.

2
  • I don't quite understand what you're doing there. But you probably want to create a javascript variable that gets incremented for each new row the user adds. Then submit that value when the user submits the rows. Then your Perl script will know how many rows there are. That's one possible approach. I don't know how in Perl, but there should be a way to find the number of fields submitted by the form, which you can use in your loop. Commented Mar 12, 2013 at 4:33
  • I need to insert one database table record per row the user adds. Per row I'm inserting rate, rangew minimum and range maximum. So if the user hits the button 3 times to add 3 "tiers" I need to insert three records via a loop I"m assuming. But what can I use to perform the loop? Can i just use the variable I declared in JQuery? That doesn't seem to work for me. Commented Mar 12, 2013 at 13:38

1 Answer 1

1

When your jQuery adds a new row, increment a counter variable in your form, then in your perl code use that to loop through your vars (this will work best as a post but you can use a get if you need to) and read up each line you need.

Put another way, in your HTML form create an input field that is hidden named tierCount Then in your jQuery to add a new row update it to the number in place
You also need to only use 1 increment variable when adding the new rows

then in your perl script you read the form value of tierCount and loop through the form vars reading them in one set at a time

<input type="hidden" name="tierCount" value="1" />  
\$('<tr id="inctier'+ (raisetierid++) +'"><TD ALIGN=left><font color="#ADD8E6">Tier '+ (raisetierid) +' Rate</TD><TD ALIGN=left><input type="text" name="tierrate'+ (raisetierid) +'" size=10 value="00.00" ></TD><TD ALIGN=left><font color="#ADD8E6">Tier '+ (raisetierid) +' Range</TD><TD ALIGN=left><font color="#ADD8E6">Minimum&nbsp;<input type="text" name="rangemin'+ (raisetierid) +'" size=10 value="" ><font color="#ADD8E6">&nbsp;Maximum&nbsp;<input type="text" name="rangemax'+ (raisetierid) +'" size=10 value="" ></TD>').appendTo("#inctier1");  

$('#tierCount').val(raisetierid);

my $tierCount = $form{"tierCount"}
# Since we start with tier 1 start at 1 instead of the usual 0
for (my $i = 1; $i <= $tierCount; $i++)
{
    my $rate = $form{"rateField" . $i};
    my $range = $form{"rangeField" . $i};
    # Etc, etc
    # then do your insert here for that row
}  

Note that the code above does not have any error checking in place

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.