mister_littlejeans Posted September 10, 2008 Share Posted September 10, 2008 Hi all, I have a registration script that I'm currently working on that needs to have the form inputs validated prior to being submitted. All the input validations are working swimmingly, other than my radio inputs for the "gender" variable ($g) I tried doing something terribly simple: if (!$g) { echo 'please enter your gender'; } ...but this simply placed the error message at the top of the page before the inputs were submitted. Then I tried to copy the preg_match validation from the other inputs. I simply copied this from a tutorial and am thinking this should work, provided I accurately describe what to look for (which I need help with). A example that works perfectly is: // Check for an email address: if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); } else { echo '<p class="error">Please enter a valid email address!</p>'; } Therefore I'd like to do something similar for my radio button input, but am not sure what to ask it to look for: // Check the gender: if (preg_match ('NOT SURE WHAT TO PUT HERE', $trimmed['gender'])) { $g = mysqli_real_escape_string ($dbc, $trimmed['gender']); } else { echo '<p class="error">Please enter your gender!</p>'; } The input names of the radio buttons are 'male', 'female' and 'transgender' The entire code (for your reference) is below: Any help with what I should put in the preg_match line would be hugely appreciated. Thanks, mister_littlejeans <?php require_once ('includes/config.inc.php'); include ('includes/header.html'); if (isset($_POST['submitted'])) { require_once (MYSQL); // Trim all the incoming data: $trimmed = array_map('trim', $_POST); // Assume invalid values: $un = $fn = $ln = $e = $p = $g = FALSE; // Check for a username: if (preg_match ('/^[A-Z \'_.-]{2,20}$/i', $trimmed['username'])) { $un = mysqli_real_escape_string ($dbc, $trimmed['username']); } else { echo '<p class="error">Please enter your username!</p>'; } // Check for a first name: if (preg_match ('/^[A-Z \'.-]{2,20}$/i', $trimmed['firstname'])) { $fn = mysqli_real_escape_string ($dbc, $trimmed['firstname']); } else { echo '<p class="error">Please enter your first name!</p>'; } // Check for a last name: if (preg_match ('/^[A-Z \'.-]{2,40}$/i', $trimmed['lastname'])) { $ln = mysqli_real_escape_string ($dbc, $trimmed['lastname']); } else { echo '<p class="error">Please enter your last name!</p>'; } // Check for an email address: if (preg_match ('/^[\w.-]+@[\w.-]+\.[A-Za-z]{2,6}$/', $trimmed['email'])) { $e = mysqli_real_escape_string ($dbc, $trimmed['email']); } else { echo '<p class="error">Please enter a valid email address!</p>'; } // Check for a password and match against the confirmed password: if (preg_match ('/^\w{4,20}$/', $trimmed['password1']) ) { if ($trimmed['password1'] == $trimmed['password2']) { $p = mysqli_real_escape_string ($dbc, $trimmed['password1']); } else { echo '<p class="error">Your passwords did not match!</p>'; } } else { echo '<p class="error">Please enter a valid password!</p>'; } // Name gender variable: $g = $_POST['gender']; } if ($un && $fn && $ln && $e && $p && $g) { // If everything's OK... // Make sure the email address is available: $q = "SELECT userid FROM users WHERE email='$e'"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_num_rows($r) == 0) { // Available. // Create the activation code: $a = md5(uniqid(rand(), true)); // Add the user to the database: $q = "INSERT INTO users (username, email, pass, firstname, lastname, gender, active, registration_date) VALUES ('$un', '$e', SHA1('$p'), '$fn', '$ln', '$g', '$a', NOW() )"; $r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc)); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Define email variables: $to = $trimmed['email']; $subject = 'Confirm your registration on My Site'; $body = ' <html> <body> <a href="http://www.mysite.com">Thank you</a> for registering at My Site. To activate your account, please click on this link: </body> </html>'; $body .= BASE_URL . 'activate.php?x=' . urlencode($e) . "&y=$a"; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; $headers .= 'From: My Site <registration@mysite.com>' . "\r\n"; // Send the email mail($to, $subject, $body, $headers); // Finish the page: echo ''; include ('includes/thankyou.html'); // Thank you email content. exit(); // Stop the page. } else { // If it did not run OK. echo '<p class="error">There was a system error adding you to the database. Please try again</p>'; } } else { // The email address is not available. echo '<p class="error">That email address has already been registered. If you have forgotten your password, use the link at right to have your password sent to you.</p>'; } mysqli_close($dbc); } // End of the main Submit conditional. ?> <html> <form enctype="multipart/form-data" action="register.php" method="POST"> User ID: <input type="text" name="username" size="30" maxlength="20" value="<?php if (isset($trimmed['username'])) echo $trimmed['username']; ?>" /><br> First Name: <input type="text" name="firstname" size="30" maxlength="20" value="<?php if (isset($trimmed['firstname'])) echo $trimmed['firstname']; ?>" /><br> Last Name: <input type="text" name="lastname" size="30" maxlength="40" value="<?php if (isset($trimmed['lastname'])) echo $trimmed['lastname']; ?>" /><br> Gender: <INPUT TYPE="radio" NAME="gender" value="female" <?echo ($_POST['gender'] == 'female') ? 'checked' : ''; ?>> Female <br> <INPUT TYPE="radio" NAME="gender" value="male" <?echo ($_POST['gender'] == 'male') ? 'checked' : ''; ?>> Male <br> <INPUT TYPE="radio" NAME="gender" value="transgender" <?echo ($_POST['gender'] == 'transgender') ? 'checked' : ''; ?>> Transgender </font><br> Email Address: <input type="text" name="email" size="30" maxlength="80" value="<?php if (isset($trimmed['email'])) echo $trimmed ['email']; ?>" /><br> Password: <input type="password" name="password1" size="30" maxlength="20" /><br> Confirm Password: <input type="password" name="password2" size="30" maxlength="20" /><br> <input type="image" name="submit" src="http://www.mysite.com/images/REGISTER_BUTTON.gif" border="0"> <input type="hidden" name="submitted" value="TRUE" /> </form> </html> Quote Link to comment https://forums.phpfreaks.com/topic/123587-quick-form-validation/ Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 All you need is: if(!strlen($trimmed['gender'])) { // no gender selected - display error } Quote Link to comment https://forums.phpfreaks.com/topic/123587-quick-form-validation/#findComment-638220 Share on other sites More sharing options...
mister_littlejeans Posted September 10, 2008 Author Share Posted September 10, 2008 Hi Neil, Thanks very much. I'm having trouble figuring out where to put this so that the error message doesn't appear when the page is first loaded (before the user hits submit) Thanks, mister_littlejeans Quote Link to comment https://forums.phpfreaks.com/topic/123587-quick-form-validation/#findComment-638229 Share on other sites More sharing options...
JonnoTheDev Posted September 10, 2008 Share Posted September 10, 2008 After: // Assume invalid values: $un = $fn = $ln = $e = $p = $g = FALSE; Quote Link to comment https://forums.phpfreaks.com/topic/123587-quick-form-validation/#findComment-638231 Share on other sites More sharing options...
mister_littlejeans Posted September 10, 2008 Author Share Posted September 10, 2008 Neil, You're a gentleman and a scholar. Thanks SO MUCH. mister_littlejeans Quote Link to comment https://forums.phpfreaks.com/topic/123587-quick-form-validation/#findComment-638233 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.