質問

My html is like this

<form name="time" method="POST" action="add.php">
    <input type="text" name="empcode" id="empcode" class="textbox" placeholder="822"/><br />
    <input type="text" name="totaltime" id="totaltime" class="textbox" value = "50" /><br />
    <strong>Start Date</strong><input type="date" name="sday"><br />
    <strong>End Date</strong><input type="date" name="eday"><br />

    <input type="submit" class="submit-button" value="Submit"/>
</form>

When user fill data in $_POST['sday'] and $_POST['sday'] the add.php file will include file.php otherwise file2.php

I tried this in add.php

if(isset($_POST['sday']) && isset($_POST['eday'])){
    include('file1.php');
}
else{
    include('file2.php');
}

But its not working should I check with zero value ?

役に立ちましたか?

解決

Use !empty() instead of isset(). those fields will always be set, at least to an empty string.

if ( !empty($_POST['sday']) && !empty($_POST['eday']) ) {

他のヒント

isset() only checks whether a variable is defined and not equal to null. If a posted value is not filled in, it will most likely be an empty string.

Therefore, you should check for empty strings, too:

if (isset($_POST['sday'], $_POST['eday']) && 
        strlen($_POST['sday']) && 
        strlen($_POST['eday'])) {

Why not empty()?

You could use empty() as well, but due to empty('0') yielding true you would need an extra condition leading to more cumbersome code.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top