CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
How Can i Back Data Form CSV File To Form?

 



webhay
Novice

Dec 4, 2000, 6:50 AM

Post #1 of 6 (1051 views)
How Can i Back Data Form CSV File To Form? Can't Post

I Write Pro. that Save Data from Form To CSV file But I want to back data From FIle to form.My problem in
-Radio And check Box

please help
haytham

------------------
Haytham Taher


Rivotti
User

Dec 13, 2000, 5:20 AM

Post #2 of 6 (1049 views)
Re: How Can i Back Data Form CSV File To Form? [In reply to] Can't Post

Hi webhay,

These could not be the better way but you can use it.

HTML
#Ex. Radio Group (3 choices)
<input type="radio" name="Radio" value="choice 1" checked>
<input type="radio" name="Radio" value="choice 2">
<input type="radio" name="Radio" value="choice 3">
# Check box
<input type="checkbox" name="check" value="ON">

CSV FILE (fields separated by \t line end \n)
#Radio #Chech
Choice 1 \t ON \n
Choice 3 \t OFF(or undef) \n
Choice 2 \t ON \n

CGI
# Grab the line, split the field and associate with respective variables Ex: $radio and $check

$radio_1 = "";
$radio_2 = "";
$radio_3 = "";
$check_1 = "";

# Check $radio value
if($radio eq "Choice 1) { $radio_1 = "checked" }
elsif($radio eq "Choice 2) { $radio_2 = "checked" }
elsif($radio eq "Choice 3) { $radio_3 = "checked" }

# Check checkbox value
if($check eq "ON") { $check_1 = "checked" }

# Print HTML

print '<input type="radio" name="Radio" value="choice 1" '.$radio_1.'>
<input type="radio" name="Radio" value="choice 2" '.$radio_2.'>
<input type="radio" name="Radio" value="choice 3" '.$radio_3.'>
<input type="checkbox" name="check" value="ON" '.$check_1.'>';

This is a messy way to do it. I normaly use HTML::Template do to the output.

Rivotti



zanardi
journeyman

Dec 15, 2000, 3:10 PM

Post #3 of 6 (1042 views)
Re: How Can i Back Data Form CSV File To Form? [In reply to] Can't Post

# Check $radio value
if($radio eq "Choice 1) { $radio_1 = "checked" }
elsif($radio eq "Choice 2) { $radio_2 = "checked" }
elsif($radio eq "Choice 3) { $radio_3 = "checked" }

# Check checkbox value
if($check eq "ON") { $check_1 = "checked" }

This could be better written as (besides that code would give a 500):

$radio_1 = "checked" if $radio eq 'Choice 1';
$radio_2 = "checked" if $radio eq 'Choice 2';
$radio_3 = "checked" if $radio eq 'Choice 3';
$check_1 = "checked" if $check eq 'ON';

Just a thought


Rivotti
User

Dec 16, 2000, 4:11 AM

Post #4 of 6 (1037 views)
Re: How Can i Back Data Form CSV File To Form? [In reply to] Can't Post

You're right zanardi, thanks for the correction. ;)

Rivotti



japhy
Enthusiast / Moderator

Dec 16, 2000, 8:06 AM

Post #5 of 6 (1035 views)
Re: How Can i Back Data Form CSV File To Form? [In reply to] Can't Post

Since the radio button can only have one value attributed to it (technically), I would do away with $radio_1, $radio_2, and $radio_3, and just have a variable which states which radio button was selected.


Code
for (1, 2, 3) { 
$rnum = $_, last if $radio eq "Choice $_";
}

If the values are more complex than "Choice 1", "Choice 2", etc., then put them in an array, and go over it by index (yes, by index!):


Code
@hated_food = ( 
"asparagus", "arugula",
"brussel sprouts", "spinach",
"pig's feet", "liver",
);

for (0 .. $#hated_food) {
$rnum = $_ if $radio eq $hated_food[$_];
}

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


Rivotti
User

Dec 16, 2000, 7:22 PM

Post #6 of 6 (1029 views)
Re: How Can i Back Data Form CSV File To Form? [In reply to] Can't Post

Hi japhy

This is what I love in perl - the way in turns simple. Thanks for increasing my knowledge

Rivotti


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives