In my form pages I use regular expressions for items which need to be typed in by users, below is an example // Check for an email address. if (eregi ('^[[:alnum:]][a-z0-9_\.\-]*@[a-z0-9\.\-]+\.[a-z]{2,40}$', stripslashes(trim($_POST['email'])))) { $e = escape_data($_POST['email']); } else { $e = FALSE; echo '<p>Please enter a valid email address!</p>'; } Code (markup): but I would like to know how I can adapt this code for use on items where a user has to choose prepared options, like radio buttons, and items in arrays.
You won't need regular exressions in this case because the user can't give other inputs than the prepared ones, so he won't input other things like invalid email adresses, malformed URL-s or malitious code. And to "see" from the script what a user has chosen, take this example with radio buttons: The form: <input type="radio" name="form1" value="ABC"> abc<br> <input type="radio" name="form1" value="DEF" checked> def<br> <input type="radio" name="form1" value="GHI"> ghi The processing: echo $_POST['form1']; // will print ABC or DEF or GHI If there is no option set by the user, you can verify using isset($_POST['form1']) .
I think he's trying to just check the email in the form for some validity. It's a psuedo fool proof method. The only foolproof method is to send a validation email. Try this code. if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", stripslashes(trim($_POST['email'])))) { $e = escape_data($_POST['email']); } else { $e = FALSE; echo '<p>Please enter a valid email address!</p>'; } PHP: