469,290 Members | 2,009 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,290 developers. It's quick & easy.

highlight text box when validation fails using php

pradeepjain
563 512MB
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.
Aug 31 '09 #1
7 10667
zorgi
431 Expert 256MB
Maybe you could simply change the style/class of the input element. For example:

Expand|Select|Wrap|Line Numbers
  1. /*Some css*/
  2. <style>
  3. .errorClass{
  4.  border: solid 1px red;
  5. }
  6. .defaultClass{
  7. /*defaults*/
  8. }
  9. </style>
  10.  
  11. <?php
  12. if($error){
  13.  $class = "errorClass";
  14. }else{
  15.  $class = "defaultClass";
  16. }
  17. ?>
  18.  
  19. <input type="text" class="<?php echo $class?>">
  20.  
Aug 31 '09 #2
pradeepjain
563 512MB
But how will it recognize the particular field for which error has occurred! Must it be repeated for all the fields?
Aug 31 '09 #3
zorgi
431 Expert 256MB
It would be easier if we could see your code.
Aug 31 '09 #4
pradeepjain
563 512MB
Expand|Select|Wrap|Line Numbers
  1.  <script type="text/javascript" src="/sample/datetimepicker_css.js"></script>
  2. <h2 class="bar"></h2>
  3. <br/>
  4. <?php
  5. include_once('valid-scripts/validateData.php');
  6. function showForm($error=false){
  7. $errors='';
  8. if($error['name']==TRUE){
  9. $errors.="<li>Error  in name</li>";
  10. }
  11.  
  12. if($error['address']==TRUE){
  13. $errors.="<li>Error  in Address</li>";
  14. }
  15.  
  16.  
  17. if($errors){
  18. $class = "valid-error";
  19. ?>
  20. <div id="errors">
  21. <ul>
  22. <?php echo $errors; ?>
  23. </ul>
  24. </div>
  25. <?php } 
  26. ?>
  27. <form id="new" name="patient_record" method="POST" action=<?php $_SERVER['php_self'] ?>>
  28. <fieldset id="personal">
  29. <legend>PERSONAL INFORMATION</legend>
  30. <label for="Name">Name : </label>
  31. <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">
  32. <br/>
  33. <label for="Address">Address : </label>
  34. <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>
  35. </fieldset>
  36.  
  37. <p>
  38.   <input id="button1" type="submit" name='submit' value="Submit Record" /> 
  39. </p>
  40. </form>
  41. <?php
  42. }
  43. if (!isset($_POST['submit'])) {
  44.  
  45. showForm();
  46. } else {
  47. $_POST    = snipExtras($_POST);
  48. $error=array();
  49. if(!validate_name($_POST['name'])) $error['name']=true;
  50. if(!validate_address($_POST['address'])) $error['address']=true;
  51. if($error){
  52. showForm($error);
  53. } else {
  54. echo 'Submission was success!';
  55. }
  56. }
  57. ?>

here you go
Aug 31 '09 #5
zorgi
431 Expert 256MB
Ok

First make your CSS:
Expand|Select|Wrap|Line Numbers
  1. <style>
  2. .error{
  3.     border: 1px solid red;    
  4. }
  5. </style>
  6.  
Than do this for name input:

Expand|Select|Wrap|Line Numbers
  1. if($error['name']==TRUE){
  2.     $errors.="<li>Error  in name</li>";
  3.     $name_class = "class='error'"; //this is added
  4. }
  5.  
Than for your input element do this:

Expand|Select|Wrap|Line Numbers
  1. <input type="text" name="name" <?php echo $name_class;?> />
  2.  
I am sure this can be done differently but considering code you produced here this is probably the quickest way. Hope this helps.
Aug 31 '09 #6
pradeepjain
563 512MB
Can you tell the other way of doing it!!i want to make the code standardised! I am ready to re modify the code!
Sep 1 '09 #7
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:
Expand|Select|Wrap|Line Numbers
  1. $name_class = "class='error'";
  2. $email_class = "class='error'";
  3. $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...
Expand|Select|Wrap|Line Numbers
  1. $name_class = "class='default'";
  2. $email_class = "class='default'";
  3. $msg_class = "class='default'";
Oct 24 '12 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

13 posts views Thread by David Morgan | last post: by
5 posts views Thread by Merx | last post: by
1 post views Thread by Joel Barsotti | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.