Articles in the Code Snippets Category
Posted: Jan 17 | Filed under: Code Snippets
If you want to validate a form and you have to check if all variables are set you have to keep a few things in mind:
a) If you disable a input field so users can only see the content you will not be able to fetch the value via $_POST
disabled=”disabled” if you add this to a input field and you submit the form you won’t be able to fetch it via $_POST
Instead use readonly=”true”
b) Textareas are slightly different. You will first have to strip all slashes if you want to fetch the real value of it.
Posted: Oct 17 | Filed under: Code Snippets, PHP
Let’s say you got a form and you don’t want to allow that people submit certain words. How would you do that? Right you will have to use the PHP function strpos. Unfortunately you can’t feed strpos with arrays (a list of needles)!
Here is a very simple solution that you can use to to check multiple needles and their occurence in a specific string.
$haystack = $myhaystack; $needle = array('badword','badword','badword','badword'); foreach($needle as $value){ $pos = strpos($haystack, $value); if ($pos !== false){ /* If we find one of those bad words do the following */ header("Location: http://mysite.com?error=badcontent"); exit; } }
I used the “header” command to redirect people to a specific site if they enter those bad words. You can output a customized error message by adding a variable at the end of the URL e.g. the variable “error”. Simply retrieve the variable via GET and then echo an appropriate message.
If you have any question feel free to ask!
Enjoy


