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

Hello,

I need some help with this basic if then check

The first line works, and if you can write the correct
syntax for this
IF/THEN thing, it will work.

{if (index($fl, '1') != -1) $flag=1; else $flag=0; \n;}
This is placed where the other field comparisons are made,
and the variable
$flag is read where it decides
to show the buttons, shows if $flag=1 and
does not show if $flag=0.

Also, how do I make variables to a form properly, as in:
<input type=hidden name=phone_number value= $ph >

or even to se it as an initial value of the variable?

Title edit by tye

Replies are listed 'Best First'.
Re: Correct Syntax
by DamnDirtyApe (Curate) on Aug 08, 2002 at 22:41 UTC
    1. You need curly braces around the code to be executed within the conditional.
      if ( index( $fl, '1' ) != -1 ) { $flag = 1 ; } else { $flag = 0 ; }
    2. There are many ways to do this. Here's a few, in the order I prefer them:

      1. Use the CGI module from CPAN.
        print $query->hidden( -name => 'hidden_name', -default => $ph );
      2. Use printf.
        printf '<input type="hidden" name="phone_number" value="%s">', $ph ;
      3. Use interpolation.
        print "<input type=\"hidden\" name=\"phone_number\" value=\"$ph\">" ;

    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche

      print "<input type=\"hidden\" name=\"phone_number\" value=\"$ph\">" ;

      perlop.

      print qq[<input type="hidden" name="phone_number" value="$ph">];

      - Yes, I reinvent wheels.
      - Spam: Visit eurotraQ.
      

      You'll probably laugh but
      How do I embed that in the form
      if ($ag eq "*********") {print " <form action=FormMail.pl method=post name=accept_estimate_cost target= +_self id=accept_estimate_cost> <input type=hidden name=job_number value= > {print qq[<input type="hidden" name="phone_number" value="$ph">]; <input type=hidden name=phone_number value= $ph >

        You're *guessing* syntax again. That will not work. Not today, not ever. If you have to ask for help for things like this, stop asking and start *learning*. You do not learn a language only by trying something, hoping the receiving end will be able to decrypt your illogical grammar. You will have to read documentation *before* you start programming. perlintro is a good start, but don't stop there.

        Sane programming cannot be done with the copy/paste function.

        If you're not willing to learn basic syntax rules, just please stop asking these stupid questions.

        - Yes, I reinvent wheels.
        - Spam: Visit eurotraQ.
        

        If you mean "in the form" in the "right in the HTML" sense, you may need to use something like Text::Template to put this kind of functionality in there.

        If you mean in the context of CGI, then the idea is simply to output bits of HTML, one after the other. In this case, you're merely printing a part conditionally.
Re: Correct Syntax
by Juerd (Abbot) on Aug 08, 2002 at 22:32 UTC

    I doubt you have even seen the fuc..-err friendly manual. Go read perlsyn for the if/else construct and perlop for the variable expansion.

    - Yes, I reinvent wheels.
    - Spam: Visit eurotraQ.
    

Re: Correct Syntax
by hacker (Priest) on Aug 09, 2002 at 12:24 UTC
    Or there's always the conditional (ternary) method:
    (index( $fl,'1')!= -1) ? $flag=1 : $flag=0;
      or you could do..
      $flag = (index( $f1,'1') == -1) ? 0 : 1;
      Dumps the extra "$flag"s and the negative conditional. Actually uses the returned value from the ternary.
        $flag = 0+(index( $f1,'1') != -1)