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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help with checkbox form data to mysql 1

Status
Not open for further replies.

rgpmedia

Programmer
Mar 6, 2005
7
US
hello I am at my wits end. I am having trouble processing multi-select lists and checkboxes then sendind the form data to my sql database. it creates/updates the record fine. however it never post all the data from the checked boxes, uses only one. I am using perl/cgi scripts. Here is 1 sample what I am using. I only included the important parts of the code for each. but no matter how many boxes are checked on one value is returned. I would greated appreciate any solutions

Mike

[html form]
<form action="quest_done.cgi" method="post">
1. What location do you fantasize about for a scene?</B>
</font><br>
<input type=checkbox name=QUESTIONlocation_encounter value=01 > A dungeon<br>
<input type=checkbox name=QUESTIONlocation_encounter value=02 > An old castle<br>
<input type=checkbox name=QUESTIONlocation_encounter value=03 > The beach<br>
<input type=checkbox name=QUESTIONlocation_encounter value=04 > A moving vehicle (i.e. car)<br>
<input type=checkbox name=QUESTIONlocation_encounter value=05 > My bed<br>
<input type=checkbox name=QUESTIONlocation_encounter value=06 > An airplane<br>
<input type=checkbox name=QUESTIONlocation_encounter value=07 > A dark back alley<br>
<input type=checkbox name=QUESTIONlocation_encounter value=08 > A movie theatre<br>
<input type=checkbox name=QUESTIONlocation_encounter value=09 > A funeral home<br>
<input type=checkbox name=QUESTIONlocation_encounter value=10 > A medieval torture chamber<br>
<input type=checkbox name=QUESTIONlocation_encounter value=11 > Under a waterfall<br>
<input type=checkbox name=QUESTIONlocation_encounter value=12 > My desk at work<br>
<input type=checkbox name=QUESTIONlocation_encounter value=13 > Elevator<br>
<input type=checkbox name=QUESTIONlocation_encounter value=14 > A swimming pool or hot tub<br>
<input type=checkbox name=QUESTIONlocation_encounter value=15 > A store dressing room<br>
<input type=checkbox name=QUESTIONlocation_encounter value=16 > The mnamedle of Central Park<br>
<input type=checkbox name=QUESTIONlocation_encounter value=17 > A Church<br>
<input type=checkbox name=QUESTIONlocation_encounter value=18 > Different time period (past or present)<br>
<input type=submit name=submit value=answer>
</form>

[quest_done.cgi]

&form_parse;
$location = $FORM{'QUESTIONlocation_encounter'};
#
use DBI;
$dbh = DBI->connect("dbi:mysql:$mysqldatabase","$mysqlusername","$mysqlpassword") || die("Couldn't connect to database!\n");
print "Content-Type: text/html\n\n";
#
&savedata;
&printconfirmation;
$dbh->disconnect;
#
sub savedata {
$query = "REPLACE INTO Question values('$locations')";
$dbh->do($query);
}
#
sub form_parse {
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/<!--(.|\n)*-->/ /g;
$value =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
$FORM{$name} = $value;
}
}
#
 
To start with i think you have a typo.
Correct me if i am wrong

you have this

$location = $FORM{'QUESTIONlocation_encounter'};

and then you have this

$query = "REPLACE INTO Question values('$locations')";

shouldn't be $location inside the last () ?

A suggestion is to use

use strict;
use warnings;

this will might help a bit for the start.

then you can make the regex part a little more simple than that.
Lets say like this

Code:
$value=~ s/%(..)/pack("c",hex($1))/ge;
instead of
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg

and why do you use these?

$value =~ s/<!--(.|\n)*-->/ /g;
$value =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
 
hy there perluserpengo

Can you show a complete example of what you mean, I honeselty dont understand anything I am coding. I work from examples, see what others have done and tweak as best I can to get it tp produce the results I need. I only used

$value =~ s/<!--(.|\n)*-->/ /g;
$value =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;"

cause that is what I had seen in others form pasing scripts. I learn from example and the specifics of whot most of the code are doing elude me for the most part. If you have a simple compact way to parse the form I am open to it, but need a full example so I know where to implement it. thanks
 
hi again perluserpengo
I used you suggestion for the parse and it still only outputs 1 value
Code:
	read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
	@pairs = split(/&/, $buffer);
	foreach $pair(@pairs){
		($name,$value)=split(/=/, $pair);
		$value =~ tr/+/ /;
		$value=~ s/%(..)/pack("c",hex($1))/ge;
		$FORM{$name} = $value;
 
either change the names of the checkboxes to
something like this

Code:
<input type=checkbox name=QUESTION1 value=01 > A dungeon<br>
<input type=checkbox name=QUESTION2 value=02 > An old castle<br>
<input type=checkbox name=QUESTION3 value=03 > The beach<br>
Or add this if/else in your last sub and make it look like this
Code:
sub form_parse  {

    read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});

    @pairs = split(/&/, $buffer);

    foreach $pair(@pairs){

        ($name,$value)=split(/=/, $pair);

        $value =~ tr/+/ /;

        $value=~ s/%(..)/pack("c",hex($1))/ge;

        $FORM{$name} = $value;

    if (!defined($FORM{$name})) {

        $FORM{$name}=$value;

     }else {

        $FORM{$name} .= "\0$value";
     }
}
The new code in this subroutine is the if/else construct at the end of the last foreach loop. This code looks to see whether the key in the name/value pair that is being processed already exists in the %FORM hash; if not, the key is associated with the current value, just as in the first incarnation of this routine. If the key does exist, you don't want to erase the current value, but we want to save this one. You might tack the new value to the end of the old value (which actually may be old values), separating the two with the null character (\0). You use the null character because it never will exist in the user data; other characters might.

 
hey there I tried you form parse code and get a 500 error, Premature end of script headers, where in the code is it not complete? Thanks
Code:
sub form_parse  {

    read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
    @pairs = split(/&/, $buffer);
    foreach $pair(@pairs){
        ($name,$value)=split(/=/, $pair);
        $value =~ tr/+/ /;
        $value=~ s/%(..)/pack("c",hex($1))/ge;
        $FORM{$name} = $value;
    if (!defined($FORM{$name})) {
        $FORM{$name}=$value;
     }else {
        $FORM{$name} .= "\0$value";
     }
}
 
If you use the CGI module then you can have nice little code saving things like this:

For related checkboxes:
Code:
$qaz->checkbox_group(-name=>'group1',-values=>$ref2ArrayOfValues);

On submit all checked values are returned as a list with param name 'group_name':

Code:
@listOfCheckedBoxes = $qaz->param('group1');

The values in -values will be in @list.. if it is checked.
In your case I would use -labels to specify an array of visible label names so the -values are not used (-values would be your index numbers that you put after 'value=').

For non-related checkboxes:
Code:
$qaz->checkbox(-name=>'checkBoxName1', -checked=>0, -value='1',-label=>'Checkbox 1');

If your perl has been installed properly, take a look at mod CGI info:
Code:
perldoc cgi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top