Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Addressing Unknown Number of Variables

Status
Not open for further replies.

UnDoug

Programmer
Dec 11, 2002
14
US
I have a question regarding Perl programming. Below are a short version (with "short" being relative) and a long version (which gives more details of the specific program I'm writing).

Any help is appreciated.


The short version:

I have a web form in which I create a set of checkboxes by first querying a database for a particular item, and then creating a checkbox for each item returned by the query. The name of each checkbox field is created using the mysql table's ID field for each returned item. Thus, if my query returns the results (424, 511, and 723) the checkboxes would be named:

checkbox424
checkbox511
checkbox723

Where the 424, 511, and 723 are the ID from the mysql table.

When the form is submitted, how can I address those form fields in order to process the data? I use cgi-lib.pl, so normally address fields as follows: $input{'field_name'} (where field_name is the name of the field from the submitted form). But in my case, since I don't know the names of the checkbox fields (since there can be a varying number and the name is based on the results of a query), I've been trying to execute the same query in the form processing script that I executed in the form display script, and then address the fields like this:

[Let's say the query produces this: @aRow = ("424", "511", "723")]

$input{'$aRow[0]'}
$input{'$aRow[1]'}
$input{'$aRow[2]'}


The problem with this is that it doesn't work. I think the reason for this is because I'm putting a variable within a variable.

Can anyone tell me what I need to do to be able to access the checkbox data?




The Long Version:

I have a website where people can create their own personal profile page. Once they've created their profile, other people can view the page. I'd like to be able to have
visitors leave comments on the profile pages, so I created a small form that has a blank for the comment, and a submit button. When the submit button is clicked, the comment is
written to a database and the profile page is refreshed to display the new comment.

The problem I'm encountering is that I'd like the profile page owner to be able to selectively delete comments. They can currently edit all of the information on their profile by clicking a link that loads a page with a webform, and in the blanks of the webform is all the data that they've already submitted so that they can edit it. I can also display all of the comments that have been left for them. How I do that is to query my mysql database and pull all the comments associated with that profile's ID #, then I read each value from the resulting array and display each comment as a row in a table. I tried giving each comment a checkbox which could be unchecked in order to delete it--I assigned the checkbox field a name based on the ID# from the comment table, and also tried assigning it a name based on it's place in the array of comments (thus the third comment
would be given the name "comment3").

This all looks good and works, up to the point where the form is submitted and the submission has to be processed. The problem I'm having is that I don't know how to reference the checkbox fields from the comments since I don't know how many comments there will be. Here's an example:

On the profile for John Doe, there are five comments. When John goes to edit his profile, he sees the 5 comments and each one has a checkbox next to it. The form fields corresponding to the comments get the field names "comment1", "comment2", "comment3", "comment4", and "comment5". John decides to delete the third comment, so he checks the checkbox next to it and thus, the field "comment3" will now have it's value set to "on".

He clicks submit.

How do I make my Perl script aware of the field names associated with the comments? Since each profile editing page may have a differing number of comments, I can't hard code this in any way that I'm familar with.
 
Code:
$input{'$aRow[0]'}
$input{'$aRow[1]'}
$input{'$aRow[2]'}
The reason this isn't working is because you're passing the literal strings '$aRow[0]', and not their values.
Try this, and see how you get on
Code:
$input{$aRow[0]}
$input{$aRow[1]}
$input{$aRow[2]}

I'd suggest investigating CGI.pm instead of cgilib.pl, as its more up to date

HTH
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Paul,

I think that's got it for me. Thanks. I really thought I had tried that.

I realize CGI.pm is more up-to-date. I just haven't had time to familiarize myself with it.

Thanks, again!
 
You're welcome, on the CGI.pm thing, it's worth it ;-)

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top