Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    better way to pass form data?

    Hi,

    For a lond time, I have been getting passed form data using

    Code:
    my %vars=$cgi->Vars;
    and it works ok.

    However, the form I am working on now needs to pass data like a hash. It's a menu form for a restaurant with dishname, price, description and course being passed for up to 15 dishes. I need to pass the data for each dish, associated with the other data for that dish and I think the way I have ben doing it is not only not associated but is pretty verbose.

    Code:
     '5:dish_name' => 'roast beef',
              '4:course' => 'Main course',
              '14:course' => '',
              '8:course' => '',
              '1:course' => 'Starters',
              '16:description' => '',
              '13:price' => '',
              '11:dish_name' => '',
              '14:description' => '',
    that involves splitting each key on the : and the processing it all. Is there a more efficient way?

    I guess I must be getting rusty.

    If; as I compile the form, I use a consistent value for each dish, I can get all the data foreach dish, in one string. Trouble is, there won't always be a course name entered (1st value of the key), and there won't always be a description, the last value.

    So I am stuck on how best to proceed with this.

    pointers requested please.

    Code:
    $VAR1 = {
              '11' => '���',
              '7' => '���',
              '17' => '���',
              '2' => '�soup�3.00�vegetable soup',
              '1' => 'Starters�Crab & avocado cocktail, crispy baby gem, apple, lobster mayonaise.�3.00�yummy',
              '18' => '���',
              '16' => '���',
              '13' => '���',
              'menu_name' => 'Christmas Lunch',
              '6' => '���',
              'subject' => 'Menus',
              '3' => '�Spicy Seafood laksa, spring onions, glass noodles, coriander.�5.00�description here',
              '9' => '���',
              '12' => '���',
              '20' => '���',
              '15' => '���',
              '14' => '���',
              'number_of_items' => '20',
              '8' => '���',
              '4' => '���',
              'action' => 'Save',
              '19' => '���',
              '10' => '���',
              '5' => '���'
            };
    bazz
    Last edited by bazz; 09-25-2010 at 06:21 AM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    had another thought...

    when building the form, is it 'best practice' to build a hash of the keys (dishname, description, price, course) and then, when processing the data, to use that hash to associate the form field values?

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Hi Bazz,

    You seem to like to confusion.

    On the Perl side, it looks like you need to use a HoH or AoH. However, as you know html form submissions don't pass their data like that, so you need to build it yourself.

    It's not real clear but it sounds like you're planning on having a single form which will submit upto 15 dishes. To me, that's a recipe (pun intended ) for disaster. I'd have the form submit a single dish and upon submission, the returned page could have a summary of what dishes have been submitted and a new "dish" form if they want to add another dish.

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    Thanks FishMonger.

    I'd have the form submit a single dish and upon submission, the returned page could have a summary of what dishes have been submitted and a new "dish" form if they want to add another dish.
    I have a form done as you describe but some restaurants want not to have to go from form to form to input a whole menu. Ideally, they could copy their menu from a WP doc into a textarea and submit it but that introduces a load of other issues I think.

    My attempt here is a compromise between how they want it to be and how I had done it before.

    As it turns out, I can now offers both methods though I haven't benchmarked my code yet for the big 15 dish form.

    here's the code I used and I would be interested in your comments.

    Code:
      my %vars = $cgi->Vars;
    
      my $number_of_items = $vars{'number_of_items'};
      
      foreach my $item_number ( 1..$number_of_items ){
      
        while ( my ( $key, $value) = each %vars){
    	
          my ( $number, $new_key ) = split ':' , $key, 2;
    	  
            if ( $number == $item_number ){
              if ( $new_key eq 'course' ){
    	    $course = $value;
             }
             elsif ( $new_key eq 'dish_name' ){
               $dish_name = $value;		
             }
             elsif( $new_key eq 'price' ){
              $price = $value;		
             }
             elsif( $new_key eq 'description' ){
               $description = $value;
             }
          }
        }
    
        #
        # put the insert_to_db->execute here using the variables above. 
        #
      }
    Edit:
    No, I don't like confusion but I have been quite foggy lately. Memory recall seems to be erroring out a lot.


    bazz
    Last edited by bazz; 09-25-2010 at 10:43 PM.
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,871
    Thanks
    2
    Thanked 164 Times in 159 Posts
    My first recommendation is to increase your indentation. A 2 character indent is too shallow and makes it more difficult to follow the logic. A 4 or 6 char indent is much better. I think 4 is best.

    Instead of the if/elsif/elsif/elsif/etc block that assigns individual scalars, assign the data directly to a HoH.

    Something like this:
    Code:
    foreach my $item_number ( 1..$number_of_items ){
    
        while ( my ( $key, $value) = each %vars){
          
            my ( $number, $new_key ) = split ':' , $key, 2;
            
            if ( $number == $item_number ){
                $menu_item{$number}{$new_key} = $value;
            }
        }
    
      #
      # put the insert_to_db->execute here using the variables above. 
      #
    }
    Last edited by FishMonger; 09-26-2010 at 12:17 AM. Reason: Added code example


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •