Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

CGI array issues.

by ghettofinger (Monk)
on Oct 30, 2005 at 21:34 UTC ( #504048=perlquestion: print w/replies, xml ) Need Help??

ghettofinger has asked for the wisdom of the Perl Monks concerning the following question:

Wise monks,

I don't even know where to start with this. I am using the yahoo api along with CGI to return the top 100 results for a given keyword to a user via a web browser. Now what I would like to do is to put the returned results into a form, put a checkbox next to each entry, and then submit all of the results back to the script. If there is no checkbox next to the result, then it doesn't get inserted into a database. It doesn't sound to hard, until I started. Here is an example:

The html form: <form name='input' method='POST' action=''><table border="0"> <td> <INPUT TYPE=CHECKBOX NAME="include" value="1">include<P> </td><td><input type="text" name="title" value="When In Doubt, Whip It + Out" size="90"><br> <textarea name="summary" rows="2" cols="130">FOR ALL OF US WHO ARE pro +jection designers, visualists, digital lighting designers, or multime +dia artists, displaying images is the end result of our process, but +display always has to be preceded by creation. We turn most often to +the digital ...</textarea><br> <a href="" target="_blank">http:/ +/</a> <br> <input type="hidden" name="url" value=" +t_whip"><br></td></tr><table border="0"> <tr> <td width="70"> <INPUT TYPE=CHECKBOX NAME="include" value="1">include<P> </td><td><input type="text" name="title" value="Prodigem Hosting Servi +ce - lerhaupt: 5th graders re-enactment of Devo's 'Whip-It'" size="90 +"><br> <textarea name="summary" rows="2" cols="130">Prodigem launches its API + (whats this?). To celebrate, preview accounts (always free) now come + with full upload access! Instructions for downloading content from P +rodigem: Download and install BitTorrent. ... ...</textarea><br><a + href="" target="_bl +ank"></a> <br> <input type="hidden" name="url" value=" +s/torrent_690.html"><br></td></tr></table><input name="submit" type=" +submit" value="Submit"> </form>

So there is the form that gets submitted to. I am using the following to parse the input, just to kind of test it. It isn't working. Here is the code:

#!/usr/bin/perl -Tw use CGI; my $q = new CGI; my @title = $q->param('title'); my @summary = $q->param('summary'); my @url = $q->param('url'); my @include = $q->param('include'); print $q->header( "text/plain" ); while (@title || @summary || @url || @include) { $title = pop(@title); $summary = pop(@summary); $url = pop(@url); $include = pop(@include); print "$title \n $summary \n $url \n $include \n" };

The problem is, is that the checkbox data is just kind of stacked within the array, and then pushed off. So, I may only check 4 boxes in the last four results, but since I have no way to match up the boxes with the entries, it will just pop the first 4 results.

I think that I need to number the entries in the HTML form, but I am not sure if that will do it, or what to do with it afterwards. Any help, or links to similar issues would be much appreciated. I am sorry that this post isn't as well written as it should be, but I am super tired and kind of in a jam. I appreciate your help.

Thanks again,

Replies are listed 'Best First'.
Re: CGI array issues.
by BUU (Prior) on Oct 30, 2005 at 23:04 UTC
    You just need someway to match your checkboxes to the data you want to enter. The easiest way would be to set the value of the checkbox to whatever data you want to add to the database. Then you just iterate over the name of all your checkboxes and insert the value. Another way would be to key every record with a unique ID, and then just have the checkbox submit the id. The downside to this is that it requires you to store the id/result mappings someplace on disk your cgi can find them when you hit submit.
Re: CGI array issues.
by greywolf (Priest) on Oct 31, 2005 at 20:58 UTC
    You just need a way to match the checkbox with the textarea. I would just run a counter when you create the form.
    $output = qq~ <form><table> ~; $counter = 1; foreach $result (@$returned_results) { $checkname = 'include_' . $counter; $textname = 'summary_' . $counter; $urlname = 'url_' . $counter; $output .= qq~ <tr> <td><input type="checkbox" name="$checkname"></td> <td><textarea name="$textname"> yadda yadda </textarea></t +d> <td><input type="hidden" name="$urlname"></td> </tr> ~; $counter++; } $output .= qq~ </table></form> ~;
    Now when you have a checked 'include_3' and 'include_87' you know that 'url_3' and 'summary_3' and 'url_87' and 'summary_87' need to go in the database.

    mr greywolf

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://504048]
Approved by sk
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2021-05-17 03:35 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (152 votes). Check out past polls.