Checkbox Validation

Hi,

Im just wondering how i can do this:

This is a piece of the current code for the 2 checkboxes

                         
<p class="<?php if($errors_switch[0]) echo 'error-text'; else echo '';?>">*Choose a Sport:</p> 
<div class="row"> 
   <label class="fix-1">                   
   <span class="small-1">Soccer</span></label> 
   <input class="right-1" type="checkbox" id="soccer" name="soccer" value="soccer" <?php print $soccer; ?>/> 
   <label class="fix-1">
   <span class="small-1">Netball</span></label>
   <input class="right-1" type="checkbox" id="netball" name="netball" value="netball"<?php print $netball; ?>/>                                                                     
</div>

the part of php

	
	$soccer = 'unchecked';
	$cricket = 'unchecked';
	$netball = 'unchecked';

if (isset($_POST['soccer'])) {
	$soccer = $_POST['soccer'];
	
	if ($soccer == 'Soccer') {
	$soccer = 'checked';
	}
	}
	
	if (isset($_POST['netball'])) {
	$netball = $_POST['netball'];
	
	if ($netball == 'Netball') {
	$netball = 'checked';
	}
	}
if('unchecked) $errors_switch[0] = true;

What i want is basically if the user submits with no checkbox selected the ‘*Choose a Sport’ text changes to red which is my error-text class. It seems simple but i cant figure out a way.

<p class=“<?php if($errors_switch[0]) echo ‘error-text’; else echo ‘’;?>”>*Choose a Sport: </p>

Haven’t you already done that (see quote)?

I have, however its not working?

I think to add some more lines to the php so if no check boxes are checked then the error-text class shows…


$errors_switch[0] = false;

if (isset($_POST['soccer'])) {
  $soccer = $_POST['soccer'];
  if ($soccer != 'Soccer') {
    $errors_switch[0] = true;
  }
}
	
if (isset($_POST['netball'])) {
  $netball = $_POST['netball'];
    if ($netball != 'Netball') {
    $errors_switch[0] = true;
  }
}

I removed my block of code and replaced it with the code you posted but nothing happened?

Was there anything else i should change?

Did you check the HTML code that is created? Doesn’t it show the ‘error-text’ class?

And is the PHP part before the form? It’s hard to tell without the whole code?

i checked, and it didnt show the class-

take a look at the live site here for the html

or just download the code as a notepad here

Hopefully it makes sense!

I can’t see the code right now, but you might try to reverse the logic of the code I posted before:


$errors_switch[0] = true;
 
if (isset($_POST['soccer'])) {
  $soccer = $_POST['soccer'];
  if ($soccer == 'Soccer') {
    $errors_switch[0] = false;
  }
}
   
if (isset($_POST['netball'])) {
  $netball = $_POST['netball'];
    if ($netball == 'Netball') {
    $errors_switch[0] = false;
  }
}

The red text is now working

but its not letting me submit. Or save the checked box if i get another field incorrect?

Ahh, I guess that’s what you used $soccer, $cricket and $netball for?


$errors_switch[0] = true;
$soccer = 'unchecked';
$cricket = 'unchecked';
$netball = 'unchecked';
 
if (isset($_POST['soccer'])) {
  if ($_POST['soccer'] == 'Soccer') {
    $errors_switch[0] = false;
    $soccer = 'checked';
  }
}
   
if (isset($_POST['netball'])) {
  if ($_POST['netball'] == 'Netball') {
    $errors_switch[0] = false;
    $netball = 'checked';
  }
}

Thanks again but it still wouldn’t let me submit? I may need to change my overall approach to the code?