Jump to content
Larry Ullman's Book Forums

Form Input Fuction Is Modifing With Check Boxes And Select Menu


Recommended Posts

Hello...everyone...

 

In my registration form I need to add some check boxes to select more than 1 option about teachers' class option. Larry's Effortless E-Commerce with PHP and MySQL book teach us how to use form input function for text, password and taxtarea inputs. So I modified the function for my check boxes. there I need to display a error message in my registration form when a user not select at least one option from the teacher option check boxes in my register page. further I need to display my error message above the check boxes as Larry did to textarea.

 

I tried like this in my form function... but I can't get my error message... can anybody tell me where I have gone wrong????

 

this code is my form function.

 

} elseif ($type == 'checkbox') {

 if ( $name == 'option[]' ) {
  // Display the error first:
  if (array_key_exists($name, $errors)) echo ' <span class="error">' . $errors[$name] . '</span>';
  // Start creating the textarea:
  echo '<input type="' . $type . '"  name="' . $name . '"  value="1"  class="checkbox" />  Individual<br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="2"  class="checkbox" />  Small Groups  <small>( Below 10 )</small><br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="3"  class="checkbox" />  Medium Groups  <small>( Between 10 to 20 )</small><br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="4"  class="checkbox" />  Big Groups  <small>( Over 20 )</small><br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="5"  class="checkbox" />  Online Tuition<br />';
 }
 if ( $name == 'medium[]' ) {
  // Display the error first:
  if (array_key_exists($name, $errors)) echo ' <span class="error">' . $errors[$name] . '</span>';
  // Start creating the textarea:
  echo '<input type="' . $type . '"  name="' . $name . '"  value="1"  class="checkbox" />  Hindi<br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="2"  class="checkbox" />  English<br />';
  echo '<input type="' . $type . '"  name="' . $name . '"  value="3"  class="checkbox" />  Tamil<br />';
 }  
}// End of primary IF-ELSE.

 

 

These are my register.php page

 

// Check for Institute Options:
if(empty( $_POST['option'])) {
$reg_errors['option'] = 'Please select at least one tuition option';
} else {
 $option = $_POST['option'];
}
// Check for Institute medium:
if(empty( $_POST['medium'])) {
$reg_errors['medium'] = 'Please select at least one tuition medium';
} else {
 $medium = $_POST['medium'];
}

 

 

This is My HTML part in register page

 

   <div>
	<label for="option">Tutoring Option <img src="../images/required_star.png" alt="required" /> : </label>
	<div class="checkbox1">
	 <?php create_form_input('option[]', 'checkbox', $reg_errors);?>
	</div>
   </div>
   <div>
	<label for="option">Tutoring Medium <img src="../images/required_star.png" alt="required" /> : </label>
	<div class="checkbox1">
	 <?php create_form_input('medium[]', 'checkbox', $reg_errors);?>
	</div>
   </div>

 

 

any helps are greatly appreciated...

 

thank you..

Link to comment
Share on other sites

Since more than one checkbox can be checked, I would give each one a unique name. You can use a for loop in your form function. In your validation, also use a for loop to check if each one isset (checking with empty won't work). I changed 'option' to 'group' as I found it more meaningful but you can use whatever name you want.

 

You can use the same code for 'medium' by just changing a few values.

 

add this to the html part of your register page

<div>
<label for="group">Tutoring Option : </label>
<div class="checkbox1">
<?php
for ($i=1, $count=5; $i <= $count; $i++) {
 create_form_input('group' . $i, 'checkbox', $reg_errors);
 }
?>
</div>

this is the validation - I added the error message to group[1] to position it above the list.

$group = array();
for ($i=1, $count=5; $i <= $count; $i++) {
if (isset($_POST['group'. $i])) {
	$group[] = $_POST['group'. $i];
}
} // end for loop
if (empty($group)){
$reg_errors['group1'] = 'Please select at least one tuition option';
}

add this code to your form function right after if ($type == 'checkbox'){

if (preg_match('/group\d{1}$/', $name)) {
 $i = substr($name, -1);
 $groupVal = array(1=>'Individual', 'Small Groups  <small>( Below 10 )</small>', 'Medium Groups  <small>( Between 10 to 20 )</small>', 'Big Groups  <small>( Over 20 )</small>', 'Online Tuition');
// Display the error first:
if (array_key_exists($name, $errors)) echo ' <span class="error">' . $errors[$name] . '</span>';
echo '<input type="' . $type . '"  name="' . $name . '"  value="' . $i . '"  class="checkbox" />  ' . $groupVal[$i] . '<br />';
}
 }

  • Upvote 1
Link to comment
Share on other sites

Thank you margaux... It working properly as I expect. But this code is hard to understand....

 

if (preg_match('/group\d{1}$/', $name)) {
 $i = substr($name, -1);
 $groupVal = array(1=>'Individual', 'Small Groups &nbsp;<small>( Below 10 )</small>', 'Medium Groups &nbsp;<small>( Between 10 to 20 )</small>', 'Big Groups &nbsp;<small>( Over 20 )</small>', 'Online Tuition');
    // Display the error first:
    if (array_key_exists($name, $errors)) echo ' <span class="error">' . $errors[$name] . '</span>';
    echo '<input type="' . $type . '"  name="' . $name . '"  value="' . $i . '"  class="checkbox" />&nbsp; ' . $groupVal[$i] . '<br />';
    }
 }

 

 

can you explain how it works.... and If I use select box in a form how can I display error massege like that.... further A select box display in the page with database values..

 

any comments are highly appreciated....

 

thank you..

Link to comment
Share on other sites

The first line uses a regular expression to see if $name matches the string 'group' plus one numeral.

The second gets that numeral by taking the last character from $name, starting from the end of the

string (a positive number would start from the beginning).

Then I create an array to use for the display text for each checkbox.

After checking for an error, I just echo it all out.

 

For select, you can use something similar

if ($type == 'select') {
		 if (preg_match('/group\d{1,}$/', $name)) {
			 $data = array(0 => 'Select One', 'Value1', 'Value2', 'Value3', 'Value4', 'Value5');
			 }


	echo '<select name="' . $name . '" id="' . $label . '"';
		if (array_key_exists($name, $errors)) echo 'class="error"';
		echo '>';

Use a foreach loop to loop through your select array.

		foreach ($data as $k => $v) {
		echo "<option value=\"$k\"";
		if ($value == $k) echo ' selected="selected"' ;
		echo ">$v</option>\n";	
		} // end foreach
	echo '</select>';
	}

 

To use d/b values,

$q = ("SELECT value1, value_id FROM table");
$r = mysqli_query($dbc, $q);
?>
	<select id="value_id" name="value_id">  
	<option value=''></option>  
 <?php
	while ($row = mysqli_fetch_array($r))
	{		
	echo "<option value=\"".$row['value_id']."\">".$row['value1']."</option>\n  ";
  }  
  ?> </select>

  • Upvote 2
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.

Guest
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.

Loading...
 Share

×
×
  • Create New...