SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    highlight empty field with message

    after a form is submitted and a field is left blank, how do i display a little error message next to the empty form field?
    http://redgoals.com ... my site ... nuff said

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Salt Lake City Utah
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ??

    You mean AFTER submitting and coming back into the page?

    OR

    You mean AFTER clicking SUBMIT but before the page is left?

    OR

    You mean BEFORE clicking SUBMIT but AFTER entering or skipping data in a field?

    Big difference in what you can/can't do.

    PHP vs. JavaScript

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    he probably meant after when he said "after"

    you could have a conditional which tests for the occurence of the blank field... like this:
    PHP Code:
    <input attributes... /> <?php if $blank_field echo $err_msg?>
    this is the most basic way though and if you've many fields you're better off using an array or even a form class object...

  4. #4
    And your point is.... redgoals's Avatar
    Join Date
    Feb 2002
    Location
    Birmingham, UK
    Posts
    496
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if empty fields are present after submit, display same form but with error next to field.
    http://redgoals.com ... my site ... nuff said

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Salt Lake City Utah
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Previous post is fine.

    You would have to set the $blank_field to true or false based on form data received, and also whether we want to see this page, or do whatever it is the form is meant to do (other than just show us data that is not filled).

    My own personal preference is to do validation on the client with JavaScript.

    If you're going to do it in the page with PHP, it might be better to write a function to parse for empty variables, and if none exist go on to do what the form is meant to do, and then another function to either highlight or somehow mark the empty field.

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is what i do if your up for a little code-reading

    i use a function which takes in an array with the erroneous fields and the error messages:
    PHP Code:
    function display_form($err=array())
    {
    ?>
    <div align="center">
    <h1>Leave a Message</h1>
    <?php
    if(count($err))
    // error in previous submission
    {
        echo 
    "<span class=\"error\">There was an error in your previous submission. Please re-enter the fields highlighted in red.</span>\n";
        echo 
    "<br />\n";
        echo 
    "<ul>\n";
        foreach(
    $err as $errmsg)
        {
            echo 
    "<li>".$errmsg."</li>\n";
        }
        echo 
    "</ul>\n";
    }
    ?>
    <form name="sign" method="post" action="<?php echo $PHP_SELF?>">

    <!-- form table -->
    <table width="<?=$GLOBALS["OB_table_width"]?>" height="50%" cellspacing="1" cellpadding="5">
        <tr>
            <th width="30%" align="right"><?php if(isset($err["fname"])) echo "<span class=\"error\">"?>First Name:<?php if(isset($err["fname"])) echo "</span>"?></th>
    the function check_form() returns the array containing the key=>value pairs matched by fieldname=>error message:
    PHP Code:
    function check_form()
    {
        global 
    $HTTP_POST_VARS;

        
    $err=array();        //  array for storing erroroneous fields and error messages

        
    if(empty($HTTP_POST_VARS["fname"]))
        
    // first name cannot be empty
        
    {    
            
    $err["fname"]="You must enter your <b>first name</b>.";
        } 
    Last edited by redemption; Mar 8, 2002 at 10:36.

  7. #7
    SitePoint Enthusiast
    Join Date
    Mar 2002
    Location
    Salt Lake City Utah
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OIC

    well then, it seems that the best thing to do is something like this (for each input to be validated):

    PHP Code:
    <input name="blahblah" value="blahblahvalue"> <?= $err("blahblah")?>
    or thereabouts

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2002
    Location
    Planet Earth
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redemption
    this is the most basic way though and if you've many fields you're better off using an array or even a form class object...
    Hi redemption, do you know of any good and clean form class to use?
    Fluid Hosting LLC - High Performance Web Presence
    New!! Virtual Private Server
    ICQ: 151540687 MSN: fluidhosting_jim@hotmail.com

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    nope sorry i don't know of any but i'm sure if you ask around (try here with a new thread or the 'Scripts and Online Services' forum) someone else would...

  10. #10
    SitePoint Enthusiast ericthehalfbee's Avatar
    Join Date
    Oct 2001
    Location
    Auckland, NZ
    Posts
    46
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This articles pretty good, helped me make sense of it anyway.
    http://www.devshed.com/Server_Side/P...ass/page1.html

    Eric


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
  •