Mihail-Cosmin Munteanu
Mihail-Cosmin Munteanu

Reputation: 522

Input from datepicker breaks inputs from dropdown lists

I created a filter for a table, which worked just fine at first, but as soon as I replaced the input form of the date with a datepicker I noticed that only the filter that involves the date works and the other are ignored and I can't see any reason why it behaves like this.

This is the part that creates the dropdown lists for the filter options:

        echo "<form action='listTimes.php' method='post'>

    <table>
    <caption><b>Filter By:</b></caption>
        <tr>
            <th>Task Name</th>
            <th>Task Stage</th>
            <th>Name</th>
            <th>Date</th>
        </tr>
        <tr>
            <td>";
        // first filter option - dropdown
        $sql = "SELECT DISTINCT taskName FROM tasks";
        $result = mysqli_query($link, $sql);
            echo "<select name='taskName'>";
            echo "<option selected='selected' value='' disabled='disabled'></option>";
            while ($row = mysqli_fetch_array($result)) {
            if (isset($row['taskName'])) {
            echo "<option value='" . $row['taskName'] . "'>" . $row['taskName'] . "</option>";
                }
            }
    echo "</select>
            </td>
            <td>";
            // second filter option - dropdown
            $sql = "SELECT DISTINCT taskStage FROM taskattributes";
            $result = mysqli_query($link, $sql);

            echo "<select name='taskStage'>
            <option selected='selected' value='' disabled='disabled'></option>";
            while ($row = mysqli_fetch_array($result)) {
            if (isset($row['taskStage'])) { 
                echo "<option value='" . $row['taskStage'] . "'>" . $row['taskStage'] . "</option>";
                }
            }
            echo "</select>

            </td>
            <td>";
            // third filter option - dropdown
            $sql = "SELECT DISTINCT userName FROM users";
            $result = mysqli_query($link, $sql);

            echo "<select name='userName'>";
            echo "<option selected='selected' value='' disabled='disabled'></option>";
            while ($row = mysqli_fetch_array($result)) {
            if (isset($row['userName'])) {      
                echo "<option value='" . $row['userName'] . "'>" . $row['userName'] . "</option>";
                }
            }
         /* fourth filter option, was just a simple text input at first. Now I changed it to a datepicker, which works, but the other three filter options stopped working at this point */
      echo "</select>
            </td>
            <td>

            <input type='text' class='datepicker' name='entryDate'>

            </td>
        </tr>
    </table>   

   <div align='center'>  <input type='submit' value='Filter'></div>
    </form>

The filter mechanism behind is very poorly written, but works just fine:

    if(!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $sql = "SELECT * FROM timeEntry";
    $result = $link->query($sql);   
    }
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName']));     
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
    $result = $link->query($sql);
}
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName' AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));
    $sql = "SELECT * FROM timeEntry WHERE userName='$userName' AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
    {   
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName'])); 
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND userName='$userName' AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND taskStage='$taskStage' AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && !isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName'])); 
    $sql = "SELECT * FROM timeEntry WHERE userName='$userName'";
    $result = $link->query($sql);
    }
elseif (isset($_POST['taskName']) && !isset($_POST['taskStage']) && !isset($_POST['userName']) && isset($_POST['entryDate']))
    {
    $taskName = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskName'])); 
    $entryDate = mysqli_real_escape_string($link, htmlspecialchars($_POST['entryDate']));   
    $sql = "SELECT * FROM timeEntry WHERE taskName='$taskName' AND entryDate='$entryDate'";
    $result = $link->query($sql);
    }
elseif (!isset($_POST['taskName']) && isset($_POST['taskStage']) && isset($_POST['userName']) && !isset($_POST['entryDate']))
    {
    $taskStage = mysqli_real_escape_string($link, htmlspecialchars($_POST['taskStage']));
    $userName = mysqli_real_escape_string($link, htmlspecialchars($_POST['userName'])); 
    $sql = "SELECT * FROM timeEntry WHERE taskStage='$taskStage' AND userName='$userName'";
    $result = $link->query($sql);
    }


echo "<table class='top1' border='1'>

<tr>
<th>Entry No.</th>
<th>Task/Activity</th>
<th>Task Stage</th>
<th>User Name</th>
<th>Time Spent</th>
<th>Date</th>
<th>Edit Entry</th>
<th>Delete Entry</th>
</tr>";

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo "<tr>
        <td align='center'>"  . $row['timeId'] . "
        </td>";
        echo "<td>" . $row['taskName'] . "</td>";
        echo "<td>" . $row['taskStage'] . "</td>";
        echo "<td>" . $row['userName'] . "</td>";
        echo "<td align='center'>" . $row['timeSpent'] . "</td>";
        $originaldate = mysqli_real_escape_string($link, $row['entryDate']);
        $newdate = date("d/m/Y", strtotime($row['entryDate']));
        echo "<td align='center'>" . $newdate . "</td>";
        echo "<td align='center'><a href='editAllTimes.php?timeId=" . $row["timeId"] . "'>Edit</a></td>";
        echo "<td align='center'><a href='deleteAllTime.php?timeId=" . $row["timeId"] . "' onclick='return checkDelete()'>Delete</a></td>
        </tr>";
    }
    echo "</table>";
}

This piece of code as bad as it looks, worked just fine before adding the date picker:

<input type='text' class='datepicker' name='entryDate'>

I have noticed a strange behavior:

  1. If I delete the name attribute name='entryDate' the other filters work and the datepicker also, but the date is not recorded when I press filter and in consequence the date filter doesn't work.
  2. If I leave the name attribute name='entryDate', the other three filters stop working, and only the date filter works.

Is there any reason for this behaviour? and any solution ?

Upvotes: 1

Views: 50

Answers (1)

szako
szako

Reputation: 1301

I think the <input> element's values are always shown in $_POST despite of filled or not. You should try isset() and empty() check on the field.

Upvotes: 2

Related Questions