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]) && $_POST[$fieldname] != 0)) {
$errors[] = $fieldname;
}
}
$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);
if (mysql_affected_rows() == 1) {
// Success
$message = "The subject was successfully updated.";
} else {
// Failed
$message = "The subject update failed.";
$message .= "<br />". mysql_error();
}
} else {
// Errors occurred
$message = "There were " . count($errors) . " errors in the form.";
}
}
I have a form with ID, MENU_NAME, POSITION and VISIBLE and i’m trying to validate the form by checking if the fields are empty OR set with foreach and if i’ts empty make $errors = $fieldname… And I test IF errors is empty execute code if it has something in it don’t… but its not listening to me… Can you help me? Thank you in advance !
$errors = array();
$names = array("James","Etel","");
foreach($names as $name){
if(empty($name)){
$errors[] = "You have an unset name in your array";
}
}
if(empty($errors)){
echo "OK";
}else{
echo "You have " . count($errors) . " In your form. <br /> review the folowing fields:";
foreach ($errors as $error){
echo $error . "<br />";
}
}
Okay, so I was incorrect, empty() should work on an array. However, I still think sizeof() is likely a better candidate, simply for the fact that empty() looks to have changed recently between 5.3 and 5.4.
I have to check the change log… but i tried with sizeof and empty it returns no error end submits the form… I should look for other single page form submission methods… right?
“” actually equals something…it equals an empty string which is totally different from an empty variable! If you check empty($name) it will NOT be empty, as it actually contains a value (that is, it contains a value of no value…I know, confusing).
Wow, that’s a good attitude to take You definitely always want to learn best practices early on. Now that you mention it, the simplest way of doing it would be:
if( !array_key_exists( $fieldname, $_POST ) )
That will reduce your if statement to just a single condition. Although, I think it might actually be a million millionth of a second slower…
and when i lave the menu_name field empty $errors_array[0] == manu_name string witch is not empty but php still considers it empty (line 9) and submits an empty string in the database… WHY?
BTW thanks for helping me out learning php alone is a pain… Glad I found sitepoint…
But when I use it with a form something goes wrong… I have to get this working… but I need to sleep first because i’m tired I already submitted bad code 2 times sorry about that… And thanks for everyone’s help…