Jump to content
Larry Ullman's Book Forums

Form Function Modifing With Radio Button..

Recommended Posts

I have modified form_functions.inc.php to radio button for suit for me. But It seems its not working..


This is my code so far..


from form function


   	 if (preg_match('/sex\d{1}$/', $name)) {
           $i = substr($name, -1);
           $name = substr($name, 0, 3);
           $sex = array(1=>'Male', 'Female' );

           // Display the error first:
           if (array_key_exists($name, $errors)) {
               echo ' <span class="inline-error">' . $errors[$name] . "</span>\n";

           echo '<input type="' . $type . '"  name="' . $name . '"  value="' . $i . '" /> ' . $sex[$i] . '   ';



This is from validation code..


    // Check for gender:  
   $sex = array();
   for ($i=1, $count=2; $i <= $count; $i++) {
       if (isset($_POST['sex'. $i])) {
           $sex[] = $_POST['sex'. $i];
   } // end for loop
   if (empty($sex)){
       $reg_errors['sex1'] = 'You have NOT entered your gender type!';


This from HTML


                           	 <div class="last-child">
                                 for ($i=1, $count=2; $i <= $count; $i++) {
                                   create_form_input('sex' . $i, 'radio', $reg_errors);



radio button working.. but validation not working its always going to error message..


hope someone help me out this..


thank you.

Link to comment
Share on other sites

Your code is somewhat complicated but I think I see what you're trying to do. As you've recognised in form_functions, radio buttons must have the same name in order to be grouped together. However your validation code is testing for $_POST['sex1'] and $_POST['sex2'] which its never going to find. Try simplifying it a bit

if (!isset($_POST['sex']) && empty($_POST['sex'])) {
 $reg_errors['sex'] = 'You have NOT entered your gender type!';

Of course as written, this code will echo the error message twice, but I'll let you figure out how to rectify that.


Also, I know you may just be working on getting the site working but as you're learning, you might as well learn best practices so instead of using a bunch of   to position your radio buttons, give them a class and use css to position them.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Create New...