472,354 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

highlight text box when validation fails using php

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 11137
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>
  11. <?php
  12. if($error){
  13.  $class = "errorClass";
  14. }else{
  15.  $class = "defaultClass";
  16. }
  17. ?>
  19. <input type="text" class="<?php echo $class?>">
Aug 31 '09 #2
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
431 Expert 256MB
It would be easier if we could see your code.
Aug 31 '09 #4
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. }
  12. if($error['address']==TRUE){
  13. $errors.="<li>Error  in Address</li>";
  14. }
  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>
  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'])) {
  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
431 Expert 256MB

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

Expand|Select|Wrap|Line Numbers
  1. <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.
Aug 31 '09 #6
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

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

Similar topics

by: David Morgan | last post by:
Hello I have a little function to highlight text if it exists. Function Highlight(vFind, vSearch) Dim RegEx Set RegEx = New RegExp RegEx.Pattern = vFind RegEx.IgnoreCase = True Highlight =...
by: Merx | last post by:
Hi! Is it possible, using Javascript, to highlight (in yellow..) all the occurrences of "number: +" (regex) of the document? Merx
by: Red | last post by:
Hi, I'm not very familiar with Javascript. I usually leave that kind of stuff up to Dreamweaver, but i'm starting to need a little more than it can offer. I have an asp page which creates a...
by: john woo | last post by:
Hi I'm not good at JS, but want to get more about it. I want to use a JSP (the java code just used to get date, the rest are html and javascript), to display a table. the requirement is the...
by: PeacError | last post by:
Using Microsoft Visual Studio .NET 2003, Visual C# .NET 1.1: I apologize if this question has been addressed elsewhere, but I could not find a reference to it in the search engine for this...
by: Atara | last post by:
I am trying to convert the following code to VB .Net, I still have some gaps (the lines that are marked with (*)) and also I need an ending condition for the while loop. any help would be...
by: Joel Barsotti | last post by:
Is there anything builtin to ASP.net that allows you to tie a text box to a button so when you press enter in the text box it emulates clicking a near by button. I've coded up some client side...
by: Celeste | last post by:
Hello, I'm trying to parse the referring url for google search terms so that when this page loads it will scroll to and highlight the search term(s). Should i be using document.referrer? ...
by: HenrikL | last post by:
Hi. I have a textbox that have a binding on it with converter and validationrules. When a validation error ouccur the foreground of the textbox will change to red cause it has a style.triggers...
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made but the http to https rule only works for...
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.