how can i validate form fields before they are sent to database?
for example -> if they contain any characters, if they contain specific character or a group of characters
you should look into a javascript form validation.
The PHP way:
if ($_POST['submit']) { if ($_POST['field1'] == "") echo "Field1 is empty!"; if (!is_int($_POST['field2'])) echo "Field2 is not an integer!"; esle $_POST['field2'] = trim($_POST['field2']); }
Just an example.
Tnx, that was helpful.
Can any1 explain the $_POST thing? Whats the advantages/disadvantages etc? Ive never looked in2 it b4...thanx in advance :-)
you have the explanation here: http://www.phpbuilder.com/board/showthread.php?threadid=10232572
You can use ereg() to check the contents matches a predefined set of rules. Such as an email address:
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+'.'@'.'[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{|}~]+\.'.'[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{|}~]+$', $last)) { echo 'Is email'; }
That one is complicated but it can be much more simple. I found the above on an article about pattern-matching :http://hotwired.lycos.com/webmonkey/99/21/index4a_page3.html?tw=programming