Menu

Adding default text to your form fields

Help
Bullmax
2007-08-19
2013-06-03
  • Bullmax

    Bullmax - 2007-08-19

    I thought I would share this so you can add default text to your form fields.
    When the user clicks on the text box, the default text is wiped away so that they can begin typing. If they click away from the box, without typing anything in, it will add the default text back so that they don’t forget what was meant to be typed.

    I find it useful on my site where I have a field for "username".  If a visitor uses the form and is not a register user, I have it default text to say "Anonymous".  If they start typing in the box then decide not to put anything there, Anonymous comes back in the box.

    Step 1 for install.
    In your form1.html file, find this:
    <!- You can add a form title here -->
    &nbsp;

    After, add:
    <html>
    <head>
    <script type="text/javascript" src="util-functions.js"></script>
    <script type="text/javascript" src="clear-default-text.js"></script>
    <title>Your page title</title>
    </head>
    <body>

    At the very end of the file, add:
    </body>
    </html>

    Then find in the file your imput fields and add the "default text" and the "clear default" values:
    <input type=text name='FirstName' value='Put your default text here' class='cleardefault'>

    Step 2.
    Copy these 2 files to /use/contact folder:
    http://www.yourhtmlsource.com/examples/clear-default-text.js
    http://www.yourhtmlsource.com/examples/util-functions.js

    Make sure you name the files clear-default-text.js and util-functions.js

     
    • smarshy

      smarshy - 2007-11-23

      Do you know how you can prevent this default text from being submitted and processed as text if the textbox is not filled in?  (and not a required field)

       
      • TNTEverett

        TNTEverett - 2007-11-23

        This is pretty difficult not knowing what your processor.php looks like but the basic idea is to add this somewhere near the top of the file;

        if($_POST['field_1']=="default text") {
        $my_field1="";
        } else {
        $my_field1=$_POST['field_1'];
        }

        Then replace every other instance of
        $_POST['field_1']
        with
        $my_field1

         
    • smarshy

      smarshy - 2007-11-23

      Thanks for that,

      My Processor code looks like this, how would I go about implementing that code?:

      <?php

      $where_form_is="http://".$_SERVER['SERVER_NAME'].strrev(strstr(strrev($_SERVER['PHP_SELF']),"/"));

      // Checkbox handling
      $field_5_opts = $_POST['field_5'][0].",". $_POST['field_5'][1].",". $_POST['field_5'][2].",". $_POST['field_5'][3].",". $_POST['field_5'][4];

      if($_POST['comments']==field.defaultValue) {
      $_POST['comments']=" ";
      } else {
      $_POST['comments']=$_POST['comments'];
      };

      mail("email@email.com","Course Reps - Form submission","Hi there,

      The following information has been entered on the Course Reps Form:

      Name:: " . $_POST['name'] . "
      Email Address:: " . $_POST['email'] . "
      UoB Username:: " . $_POST['username'] . "
      Course you represent:: " . $_POST['course'] . "
      Year groups you represent:: " . $_POST['yearone'] . "   " . $_POST['yeartwo'] . "  " . $_POST['yearthree'] . "  " . $_POST['yearfour'] . "  " . $_POST['yearfive'] . "
      Any other comments:: " . $_POST['comments'] . "

      TEXT TEXT

      Thanks,

      Sam
      ");

      mail("email@email.com","Course Reps - Form submission","Hi there,

      The following has been entered on the Course Reps Form:

      Name:: " . $_POST['name'] . "
      Email Address:: " . $_POST['email'] . "
      UoB Username:: " . $_POST['username'] . "
      Course you represent:: " . $_POST['course'] . "
      Year groups you represent:: " . $_POST['yearone'] . "   " . $_POST['yeartwo'] . "  " . $_POST['yearthree'] . "  " . $_POST['yearfour'] . "  " . $_POST['yearfive'] . "
      Any other comments:: " . $_POST['comments'] . "

      TEXT TEXT

      Thanks,

      Sam
      ");

      mail($_POST['email'],"Course Reps - Thank you for your submission","Hi there,

      You have submitted the following information on the Course Reps Form:

      Name:: " . $_POST['name'] . "
      Email Address:: " . $_POST['email'] . "
      UoB Username:: " . $_POST['username'] . "
      Course you represent:: " . $_POST['course'] . "
      Year groups you represent:: " . $_POST['yearone'] . "   " . $_POST['yeartwo'] . "  " . $_POST['yearthree'] . "  " . $_POST['yearfour'] . "  " . $_POST['yearfive'] . "
      Any other comments:: " . $_POST['comments'] . "

      TEXT TEXT

      Thanks,

      Sam
      ");

      include("excel.php");

      ?>

       
    • smarshy

      smarshy - 2007-11-23

      The...

      if($_POST['comments']==field.defaultValue) { 
      $_POST['comments']=" "; 
      } else { 
      $_POST['comments']=$_POST['comments']; 
      };

      ... is me trying this out by the way :-)

       
    • smarshy

      smarshy - 2007-11-23

      I managed to get the code to work by replacing the

      field.defaultValue

      with "the actual text entered into the box"

      Sam

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.