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

Hi,
I m using cgi module. I am able to create a widget (textfiled) and set value to that field
textfield(-name=>'txt1', -id=>'txt1',-value=>'any text');
But, i want to create a widget and setting value by two separete statements,

I used the syntax to create, textfield(-name=>'txt1',-id=>'txt1'), it works fine
param('txt1','any text') or param(-name=>'txt1',-value=>'any text'),- it does not show the text 'any text' in text field, but on getting the value by param ('txt1')- it returns the text 'any text'

Regards,
Anbarasu
  • Comment on is it possible to write two statements one for creating a widget and setting value to the widget?

Replies are listed 'Best First'.
Re: is it possible to write two statements one for creating a widget and setting value to the widget?
by Corion (Patriarch) on Apr 29, 2009 at 06:37 UTC

    That's not how CGI works. CGI works by printingreturning text. Once you've called textfield(-value => $value) it printsreturns <textarea>$value</textarea>. You cannot undo that, so what you want does not work.

    Update: moritz pointed out that CGI does not implicitly print the HTML. That just shows how long ago I used it for HTML generation ;)

Re: is it possible to write two statements one for creating a widget and setting value to the widget?
by moritz (Cardinal) on Apr 29, 2009 at 06:34 UTC
    I assume you're talking about CGI.pm (please always say things like that in your postings, don't let us guess).

    textfield() simply returns a string that, when used as HTML, makes a text field appear. If you want to change that, you have to manipulate the text. But I recommend against that - create it correctly in the first place. Or use a template system instead.

Re: is it possible to write two statements one for creating a widget and setting value to the widget?
by wol (Hermit) on Apr 29, 2009 at 09:31 UTC
    But, i want to create a widget and setting value by two separete statements,
    Technically, that's possible, but the order has to be the other way round:
    # Set value my $widgetValue = "I'd like this to be the value"; # Create widget textfield(-name=>'txt1', -id=>'txt1',-value=>$widgetValue);
    But to more helpfully answer what you're trying to acheive: you're barking up the wrong tree here.

    param() reads/overwrites any value which was sent from the browser when it sent the HTTP request. By contrast, textfield() will produce an HTML string which can be sent to the browser as an HTTP response. The two functions operate completely independently, even if the name field happens to be the same.

    Thinking about it, I cannot work out what you might be trying to do. Try explaining the problem a bit more generally (rather than asking why your solution is not working) and we might be able to point you in a different direction.

    --
    use JAPH;
    print JAPH::asString();

Re: is it possible to write two statements one for creating a widget and setting value to the widget?
by Anonymous Monk on Apr 29, 2009 at 06:28 UTC
    Why?