Input form

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TJ

    Input form

    Dear PHP group,

    First question:

    I have an input form that upon pressing a button validates the values
    entered into this form. I am using the following code recall this form if
    there is an error in one of the requested fields:

    <form name="form1" method="post" action= "InputForm. php" >

    All works fine. However, if there is an error and this form is recalled, all
    the fields are blanked out when you are brought back to the form. I'd like
    for the form to retain the values that had been entered. How do I do this?

    Second question:

    For the fields that are in error, I'd like for the titles for the fields to
    display an asterisk and change the text to red. Have any of you done this
    and if so how do you implement it?

    Thanks,


  • Jochen Buennagel

    #2
    Re: Input form

    TJ wrote:[color=blue]
    > I'd like
    > for the form to retain the values that had been entered. How do I do this?[/color]

    You need to put the values that where transmitted by the form into the
    "value" of the inputs like this:

    <input type="text" name="firstname " value="<?php echo
    $_POST['firstname'];?>" />
    [color=blue]
    > For the fields that are in error, I'd like for the titles for the fields to
    > display an asterisk and change the text to red. Have any of you done this
    > and if so how do you implement it?[/color]

    <?php if (valid($_POST['firstname'], 'name')): ?>
    <font color="#ff0000" >*&nbsp;
    <?php else: ?>
    <font>
    <?php endif; ?>
    Firstname:
    </font>

    Somewhere you define a function:
    <?php
    function valid($value, $type){
    if(type=='name' ){
    // return true if this is a valid name, false otherwise
    }
    }
    ?>

    Of course, this is VERY simplistic....

    Jochen

    Comment

    • Geoff Berrow

      #3
      Re: Input form

      I noticed that Message-ID: <RRoGb.10873$JD 6.5055@lakeread 04> from TJ
      contained the following:
      [color=blue]
      >Dear PHP group,
      >
      >First question:
      >
      >I have an input form that upon pressing a button validates the values
      >entered into this form. I am using the following code recall this form if
      >there is an error in one of the requested fields:
      >
      ><form name="form1" method="post" action= "InputForm. php" >
      >
      >All works fine. However, if there is an error and this form is recalled, all
      >the fields are blanked out when you are brought back to the form. I'd like
      >for the form to retain the values that had been entered. How do I do this?
      >
      >Second question:
      >
      >For the fields that are in error, I'd like for the titles for the fields to
      >display an asterisk and change the text to red. Have any of you done this
      >and if so how do you implement it?
      >[/color]
      Check this out - it simply checks if the field has been entered or not.
      If the form is resubmitted it repopulates the textfields with the $_POST
      variables.

      <html>
      <head>
      <title>Check fields</title>
      <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
      <style type="text/css">
      <!--
      ..red { color: #FF0000}
      -->
      </style>
      </head>

      <body bgcolor="#FFFFF F" text="#000000">
      <?php
      function error($field){
      if (!$field && $_POST["Submit"]){
      print"<p class=\"red\">* ";
      }
      else{
      print"<p>";
      }
      }
      ?>
      <form name="form1" method="post" action="">
      <?php
      error($_POST["textfield"]);
      ?>
      First name <input type="text" name="textfield " value="<?php print
      $_POST["textfield"];?>">
      </p>
      <?php
      error($_POST["textfield2 "]);
      ?>
      Last name <input type="text" name="textfield 2"value="<?p hp print
      $_POST["textfield2 "];?>"></p>
      <p>
      <input type="submit" name="Submit" value="Submit">
      </p>
      </form>
      </body>
      </html>

      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • TJ

        #4
        Re: Input form

        Thanks -- I'll review your solution.

        "Jochen Buennagel" <zang.NOSPAM@bu ennagel.com> wrote in message
        news:bsd6b6$5qd $01$1@news.t-online.com...[color=blue]
        > TJ wrote:[color=green]
        > > I'd like
        > > for the form to retain the values that had been entered. How do I do[/color][/color]
        this?[color=blue]
        >
        > You need to put the values that where transmitted by the form into the
        > "value" of the inputs like this:
        >
        > <input type="text" name="firstname " value="<?php echo
        > $_POST['firstname'];?>" />
        >[color=green]
        > > For the fields that are in error, I'd like for the titles for the fields[/color][/color]
        to[color=blue][color=green]
        > > display an asterisk and change the text to red. Have any of you done[/color][/color]
        this[color=blue][color=green]
        > > and if so how do you implement it?[/color]
        >
        > <?php if (valid($_POST['firstname'], 'name')): ?>
        > <font color="#ff0000" >*&nbsp;
        > <?php else: ?>
        > <font>
        > <?php endif; ?>
        > Firstname:
        > </font>
        >
        > Somewhere you define a function:
        > <?php
        > function valid($value, $type){
        > if(type=='name' ){
        > // return true if this is a valid name, false otherwise
        > }
        > }
        > ?>
        >
        > Of course, this is VERY simplistic....
        >
        > Jochen
        >[/color]


        Comment

        • TJ

          #5
          Re: Input form

          Thanks, I'll review this.

          "Geoff Berrow" <blthecat@ckdog .co.uk> wrote in message
          news:gv8kuvoa3p ullv0oa5lrlr1tq vi8i6r9qi@4ax.c om...[color=blue]
          > I noticed that Message-ID: <RRoGb.10873$JD 6.5055@lakeread 04> from TJ
          > contained the following:
          >[color=green]
          > >Dear PHP group,
          > >
          > >First question:
          > >
          > >I have an input form that upon pressing a button validates the values
          > >entered into this form. I am using the following code recall this form if
          > >there is an error in one of the requested fields:
          > >
          > ><form name="form1" method="post" action= "InputForm. php" >
          > >
          > >All works fine. However, if there is an error and this form is recalled,[/color][/color]
          all[color=blue][color=green]
          > >the fields are blanked out when you are brought back to the form. I'd[/color][/color]
          like[color=blue][color=green]
          > >for the form to retain the values that had been entered. How do I do[/color][/color]
          this?[color=blue][color=green]
          > >
          > >Second question:
          > >
          > >For the fields that are in error, I'd like for the titles for the fields[/color][/color]
          to[color=blue][color=green]
          > >display an asterisk and change the text to red. Have any of you done this
          > >and if so how do you implement it?
          > >[/color]
          > Check this out - it simply checks if the field has been entered or not.
          > If the form is resubmitted it repopulates the textfields with the $_POST
          > variables.
          >
          > <html>
          > <head>
          > <title>Check fields</title>
          > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
          > <style type="text/css">
          > <!--
          > .red { color: #FF0000}
          > -->
          > </style>
          > </head>
          >
          > <body bgcolor="#FFFFF F" text="#000000">
          > <?php
          > function error($field){
          > if (!$field && $_POST["Submit"]){
          > print"<p class=\"red\">* ";
          > }
          > else{
          > print"<p>";
          > }
          > }
          > ?>
          > <form name="form1" method="post" action="">
          > <?php
          > error($_POST["textfield"]);
          > ?>
          > First name <input type="text" name="textfield " value="<?php print
          > $_POST["textfield"];?>">
          > </p>
          > <?php
          > error($_POST["textfield2 "]);
          > ?>
          > Last name <input type="text" name="textfield 2"value="<?p hp print
          > $_POST["textfield2 "];?>"></p>
          > <p>
          > <input type="submit" name="Submit" value="Submit">
          > </p>
          > </form>
          > </body>
          > </html>
          >
          > --
          > Geoff Berrow (put thecat out to email)
          > It's only Usenet, no one dies.
          > My opinions, not the committee's, mine.
          > Simple RFDs http://www.ckdog.co.uk/rfdmaker/[/color]


          Comment

          Working...