سؤال

I've made a page where you can set the number of rows of information you want to give in, this data goes through a function and let the number of rows appear. Now each of this information has to be sent through but first I want to check if all fields are filled in.

Generating the rows I have used a for-loop.

This is what I try to do now:

<td <?php checkfilled("datum".$x) ?> ><input size="6" type='text' name='datum<?php echo $x ?>' value='<?php setvalue2("datum".$x)?>' /></td>

The function checkfilled will try and check whether it's filled in or not.

function checkfilled($fieldname){

    if(isset($_POST['send'])){
        if($_POST[$fieldname]=="" || !isset($_POST[$fieldname])){       
            echo 'class="error"';
        }else{
            echo "";
        }
    }

}

The error class contains that the background-color will be set to green if it's not filled in.

But for somewhat odd reason it doesn't work.

Does anyone know what might be wrong and how to solve this?

هل كانت مفيدة؟

المحلول 2

Your code is fine. I ran some tests and the problem was with styling the <td> tag. You didn't specify, but I am wondering if your <td> is wrapped with <table><tr>. If not, that is the problem.

I tested your exact code, it failed as you described. Then I changed the <td> to a <div> and it worked like a charm.

Then I wrapped the <td> in a <table><tr> ... </tr></table> and it also worked fine.

So, ensure that your <td> is in a row, in a table, and your code should work.

نصائح أخرى

Use this

function checkfilled($fieldname){

if(isset($_POST['send'])){
    if(isset($_POST[$fieldname]) && !empty($_POST[$fieldname])){       
        echo 'class="success"';
    }else{
        echo 'class="error"';
    }
}
}
if(empty($_POST['fieldname'])){
echo "<div style="background:green;.....">This field is missing</div>";
}

else if(empty($_POST['another_field_name'])){
echo "<div style="background:green;.....">This field is missing</div>";
}
else{
echo "everything seems good, Let's do some math.";
}

Change

$_POST[$fieldname]=="" || !isset($_POST[$fieldname])

To:

empty($_POST[$fieldname])
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top