highlight text box when validation fails using php

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pradeepjain
    Contributor
    • Jul 2007
    • 563

    highlight text box when validation fails using php

    hii. i am developing a new form for which validation is done using php , so the error text gets displayed also,I wanted to make a improvement for form. i.e can the textfield for which validation field be highlighted with say border:red; using php or javascript needs to be used. i am asking this bcos i am not using javascript for validation purpose in this form.
  • zorgi
    Recognized Expert Contributor
    • Mar 2008
    • 431

    #2
    Maybe you could simply change the style/class of the input element. For example:

    Code:
    /*Some css*/
    <style>
    .errorClass{
     border: solid 1px red;
    }
    .defaultClass{
    /*defaults*/
    }
    </style>
    
    <?php
    if($error){
     $class = "errorClass";
    }else{
     $class = "defaultClass";
    }
    ?>
    
    <input type="text" class="<?php echo $class?>">

    Comment

    • pradeepjain
      Contributor
      • Jul 2007
      • 563

      #3
      But how will it recognize the particular field for which error has occurred! Must it be repeated for all the fields?

      Comment

      • zorgi
        Recognized Expert Contributor
        • Mar 2008
        • 431

        #4
        It would be easier if we could see your code.

        Comment

        • pradeepjain
          Contributor
          • Jul 2007
          • 563

          #5
          Code:
           <script type="text/javascript" src="/sample/datetimepicker_css.js"></script>
          <h2 class="bar"></h2>
          <br/>
          <?php
          include_once('valid-scripts/validateData.php');
          function showForm($error=false){
          $errors='';
          if($error['name']==TRUE){
          $errors.="<li>Error  in name</li>";
          }
          
          if($error['address']==TRUE){
          $errors.="<li>Error  in Address</li>";
          }
          
          
          if($errors){
          $class = "valid-error";
          ?>
          <div id="errors">
          <ul>
          <?php echo $errors; ?>
          </ul>
          </div>
          <?php } 
          ?>
          <form id="new" name="patient_record" method="POST" action=<?php $_SERVER['php_self'] ?>>
          <fieldset id="personal">
          <legend>PERSONAL INFORMATION</legend>
          <label for="Name">Name : </label>
          <input type="text" size="30"  id="name" class="<?php echo $class?>" name="name"  value="<?php echo $_POST['name'] ?>"onkeypress="javascript:indic_script_lang(event);" onkeydown="javascript:toggleKBMode(event);" onfocus="javascript:indic_script_show_typing_method();" onblur="javascript:indic_script_hide_typing_method();"><span class="star">*</span><span class="star">
          <br/>
          <label for="Address">Address : </label>
          <textarea name="address"  onkeypress="javascript:indic_script_lang(event);" onkeydown="javascript:toggleKBMode(event);" onfocus="javascript:indic_script_show_typing_method();" onblur="javascript:indic_script_hide_typing_method();"><?php echo $_POST['address'] ?> </textarea><span class="star">*</span>
          </fieldset>
          
          <p>
            <input id="button1" type="submit" name='submit' value="Submit Record" /> 
          </p>
          </form>
          <?php
          }
          if (!isset($_POST['submit'])) {
          
          showForm();
          } else {
          $_POST    = snipExtras($_POST);
          $error=array();
          if(!validate_name($_POST['name'])) $error['name']=true;
          if(!validate_address($_POST['address'])) $error['address']=true;
          if($error){
          showForm($error);
          } else {
          echo 'Submission was success!';
          }
          }
          ?>

          here you go

          Comment

          • zorgi
            Recognized Expert Contributor
            • Mar 2008
            • 431

            #6
            Ok

            First make your CSS:
            Code:
            <style>
            .error{
            	border: 1px solid red;	
            }
            </style>
            Than do this for name input:

            Code:
            if($error['name']==TRUE){
                $errors.="<li>Error  in name</li>";
                $name_class = "class='error'"; //this is added
            }
            Than for your input element do this:

            Code:
            <input type="text" name="name" <?php echo $name_class;?> />
            I am sure this can be done differently but considering code you produced here this is probably the quickest way. Hope this helps.

            Comment

            • pradeepjain
              Contributor
              • Jul 2007
              • 563

              #7
              Can you tell the other way of doing it!!i want to make the code standardised! I am ready to re modify the code!

              Comment

              • sonofjorel
                New Member
                • Oct 2012
                • 1

                #8
                I have a form with PHP Validation and this is EXACTLY the solution I've been looking for! Thank you SO MUCH!
                For my form I had to create variables for each required field:
                Code:
                $name_class = "class='error'";
                $email_class = "class='error'";
                $msg_class = "class='error'";
                as well as create rules to return each field to its "valid" default CSS state and so it is no longer highlighted in red when the validation conditions are TRUE. Also, don't forget to name these variable at the top of your PHP code so the form loads with the default CSS rules...
                Code:
                $name_class = "class='default'";
                $email_class = "class='default'";
                $msg_class = "class='default'";
                Last edited by sonofjorel; Oct 24 '12, 11:52 PM. Reason: highlighting code

                Comment

                Working...