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.
highlight text box when validation fails using php
Collapse
X
-
Tags: None
-
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?>">
-
But how will it recognize the particular field for which error has occurred! Must it be repeated for all the fields?Comment
-
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 goComment
-
Ok
First make your CSS:
Code:<style> .error{ border: 1px solid red; } </style>
Code:if($error['name']==TRUE){ $errors.="<li>Error in name</li>"; $name_class = "class='error'"; //this is added }
Code:<input type="text" name="name" <?php echo $name_class;?> />
Comment
-
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
-
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'";
Code:$name_class = "class='default'"; $email_class = "class='default'"; $msg_class = "class='default'";
Comment
Comment