Sunday, June 12, 2016

PHP Form Error Validation




I'm working on a php form with some validation rules.



If some of the fields are left blank that are require the form will report and error message and ask the user to complete the field.



The form currently keeps the values of the $_POST data so that if errors occur the data which is in the field remains.



I am having a problem with two fields(drop down lists) which are populated with data from a database.
If I complete these fields but there is a error elsewhere the form displays with the values in the drop down list but when I correct the errors and try submit the form it tells me that the drop down list fields are empty.



Here is the code for them








Any idea's why this is happening?



Answer



For starters,



You could do something like the following, excuse me if this snippet has bugs, but it should give you a general idea of how you could achieve what you want to:



    $myOptions = array(
'0' => ' --- nothing selected ---',
'1' => 'Apples',
'2' => 'Bananas',

// ...
);
?>



You can cleanup the above a bit using the alternative PHP syntax:



http://php.net/manual/en/control-structures.alternative-syntax.php



This isn't part of your bug, but just a good practice consideration, some of your HTML tags and their attributes are uppercase, and it's common nowadays to use lowercase. It's more standard this way.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...