SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Earth
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    remove text string from field value - formmail

    Hi.
    I am using a hidden field to pass to formmail empty optional fields if the users let them blank.
    The problem is that when a user fills out one of these optional fields the hidden values adds up to the user specified value.
    example:
    Code:
    <input type="hidden" name="foo" value="not specified" />
    <input type="text" name="foo" value="" />
    results in

    Code:
    foo: not specified
    when the user leaves the field blank; if the user fills out the field with the value "Hello", the final submitted value is:
    Code:
    foo: not specified Hello
    How to remove the "not specified" string from user specified values and leave it just for empty submissions?

    Thanks in advance!
    cla313

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you are thinking about the problem backwards. Use a server side script (perl, python, php, ruby, etc.) to determine whether the form field was left blank, then send whatever value you want to your utility.

    The other option is to use javascript on the client side to intercept the form before it is sent to the server, and then use javascript to assign whatever value you want to the form field.

  3. #3
    SitePoint Wizard Stomme poes's Avatar
    Join Date
    Aug 2007
    Location
    Netherlands
    Posts
    10,278
    Mentioned
    50 Post(s)
    Tagged
    2 Thread(s)
    Though be aware that someone like me would slip through that— I don't get hit by JS validation cause I have it off. It would have to be a "nice extra" regarding form validation.

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Earth
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you are thinking about the problem backwards. Use a server side script (perl, python, php, ruby, etc.) to determine whether the form field was left blank, then send whatever value you want to your utility.

    The other option is to use javascript on the client side to intercept the form before it is sent to the server, and then use javascript to assign whatever value you want to the form field.
    I'm already using a perl script to submit the form data and javascript to perform form validation. I'm speaking about optional fields.
    I would like an answer for the server side part of coding. That's why I started this thread here. I hope somebody can tell me how do that server side in perl.
    cla313

  5. #5
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably because since you have two 'foo' fields, the param data arriving at formmail.pl is an array.
    Code:
    $params{foo} = ['not specified','Hello'] #pseudo, not sure if this is the actual internal representation
    When an array is printed
    Code:
     print "@array"
    you end up with a space-separated string of items.
    You'd probably have to patch your formmail.pl to prevent against this. I've not looked at its internals.

  6. #6
    SitePoint Zealot
    Join Date
    Jun 2001
    Location
    Earth
    Posts
    111
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Jawsoflife. Thanks for the hint.

    How can I modify the code below in a way that if $name= "foo" only the second parameter is sent if this is not empty?

    subroutine building main email field:

    Code:
    sub send_main_email_field {
      my ($self, $name, $value) = @_;
     
      my ($prefix, $line) = $self->build_main_email_field($name, $value);
    
      my $nl = ($self->{CFG}{double_spacing} ? "\n" : "\n");
      if ($self->{CFG}{wrap_text} and length("$prefix$line") >  $self->email_wrap_columns) {
        $self->mailer->print( $self->wrap_field_for_email($prefix,  $line) . $nl );
      }
      else {
        $self->mailer->print("$prefix$line$nl");
      }
    }
    where this is the subroutine returning name and value pairs:

    Code:
    sub build_main_email_field {
      my ($self, $name, $value) = @_;
      return ("$name:  ", $value);
    }
    Thanks in advance!
    cla313

  7. #7
    SitePoint Member
    Join Date
    Mar 2010
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Arrow

    Hard to see from here - I have no idea what code is handling your cgi parameter untainting and such. I would filter this at the part where you start processing the parameters sent to your backend.

    Let's say your default 'undef' string is "not specified", then you could do

    Code:
    #pseudo!
    foreach my $param ( keys &#37;cgi_params ) {
        $cgi_params{$param} = ref $param eq 'ARRAY' # is this an arrayref
            ? grep $_ != 'not specified', @$param #filter out all items that are not 'undef', should be 1 item
            : $param # what we expected
        ;
    }
    which should make you end up with $cgi_params{foo} being 'Hello', from the previous post.

    I hope this points you in the right direction. Otherwise feel free to PM me with more code so I can flesh out how your backend works.

    Best of luck!


Bookmarks

Posting Permissions

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