validation code

hi
dears ,please help me below is a validation code that receive fields from a form

if(isset($_POST[‘submit’])){
$errors = array();
//form validation
// the loop is for the name of Fields that shouldnt be empty
// if there is an empty field then we put it into our errors array $
$required_fields = array(‘menu_name’, ‘position’, ‘visible’); // array contains the required fields
foreach($required_fields as $fieldname){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])&& $_POST[$fieldname] != 0){
// the $_POST[$fieldname] != 0 because we have radio input (visible) and we dont want the
$errors[] = $fieldname;
}
}

but it is not working i mean if i use only empty($_POST[$fieldname])it works for some fields but not were have true or false values like visible
please help

[php]
if(isset($_POST[‘submit’])){
$errors = array();
$required_fields = array(‘menu_name’, ‘position’, ‘visible’);
foreach($required_fields as $fieldname){
if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])) {
$errors[] = $fieldname;
}
if($_POST[‘visible’] != 0) {
$errors[] = “You must select Visible”;
}
}
}
[/php]

Just check to see if anything is empty or not set. I assume the != 0 is for the radio button? if that’s user input, then your check should fail because you’re only checking to see what’s empty, not what the value of the radio button is. Put that in another if statement.

Plus, u were missing a } at the end.

thank you Mr. richei,
yes the it is for the Radio button, i want to make sure that the user has to check yes or no…
i tried your code but it didnt work …
thank you and i appreciate ur replay

What’s the value of the radio button?

the radio button value depeneds on what the user select 0 or 1 .
below code worked but if the menu_name was empty , it will update any way it will insert an empty field

[php]<?php
if(isset($_POST[‘submit’])){
$errors = array();
//form validation
$required_fields = array(‘menu_name’, ‘position’, ‘visible’);// array contains the required fields
foreach($required_fields as $fieldname){

 if(!isset($_POST[$fieldname]) || (empty($_POST[$fieldname])&& $_POST[$fieldname] != 0)){
  $errors[] = $fieldname;
  }

}

 // check the field name length
 $fields_with_lengths = array ('menu_name' => 30);
 foreach($fields_with_lengths as $fieldname => $maxlength){
    if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $maxlength){
	 $errors[] = $fieldname; 
    }
 }
 
 if(empty($errors)){  
	 //perform update
 $id = mysql_prep($_GET['subj']);
 $menu_name = mysql_prep($_POST['menu_name']);
 $position = mysql_prep($_POST['position']);
 $visible = mysql_prep($_POST['visible']);
 
 $query ="UPDATE subjects SET
	           menu_name = '{$menu_name}',
			   position = {$position},
			   visible = {$visible} 
		WHERE id = {$id}";
			  $result = mysql_query($query, $connection);
			  // check if update was success
			  if(mysql_affected_rows() == 1){
				  //success
				  $message = "the subject was successfuly updated";
			  }else {
				  //failed
				  $message = "the subject update  failed";
				  $message .="<br/>". mysql_error();
			        }
	 
  }else {
	 //errors occurred
	 $message = "there were " .count($errors) . " errors in the form ";
        }

}// end: of if(isset($_POST[‘submit’]))
?>[/php]

any help for my topic please

Sponsor our Newsletter | Privacy Policy | Terms of Service