Reputation: 9
Good morning folks,
I have the following PHP function to create a form.
echo "<form action='dashboard_ver_3.php' method='post'>
<label> From Date: <input type='date' placeholder='-- select --' name='fromdate'></label>
<label> To Date: <input type='date' placeholder='-- select --' name='todate'></label>
<label> First: <input type='text' name='fName'></label>
<label> Last: <input type='text' name='lName'></label>
<label> Found: <select class='select' name='filterfound'></label>
<option disabled selected value> -- select -- </option>
<option value='GLASSDOOR'>Glassdoor</option>
<option value='INDEED'>Indeed</option>
<option value='CRAIGSLIST'>Craigslist</option>
<option value='MONSTER'>Monster</option>
<option value='FACEBOOK'>Facebook</option>
<option value='FRIEND'>Friend</option>
<option value='FLYER'>Flyer</option>
<option value='GOOGLE'>Google</option>
<option value='RECRUITER'>Recruiter</option>
<option value='NEWSPAPER'>Newspaper</option>
<option value='WALK-IN'>Walk-in</option>
<option value='OTHER'>Other</option>
</select>
<label>Filter by Center: <select class='select' name='filtercenter'></label>
<option disabled selected value> -- select -- </option>
<option value='CAM'>Cambridge, MA</option>
<option value='WOR'>Worcester, MA</option>
<option value='FLL'>Ft. Lauderdale, FL</option>
<option value='COL'>Columbia, MO</option>
<option value='DUR'>Durham, NC</option>
</select>
<input class='submit' type='submit' name='search' value='Search applications ...' />
</form>";
I have the following if statements to search by last name, first name, and both last and firstname. The functions within the if statements all work perfectly fine.
if(empty($_POST['filtercenter']) && isset($_POST['fName']) && isset($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>bc<br>";
echo sfName_lName();
}
if(empty($_POST['filtercenter']) && isset($_POST['fName']) && empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>b<br>";
echo sfName();
}
if(empty($_POST['filtercenter']) && empty($_POST['fName']) && isset($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>c<br>";
echo slName();
}
The issue is when I search by first name, last name, or both; all three if statements run. If I do an empty search, again, all three of these if statements run. Essentially, the fName and lName fields are always set, regardless of them being filled or not.
It is worth mentioning that the filtercenter and filterfound can be left unfilled and they are not set when posted. I am wondering why this is the case.
Upvotes: 0
Views: 43
Reputation: 26
isset() is only check the variable is exists or not. You should check with empty() and !empty().
here is the code:
if(empty($_POST['filtercenter']) && !empty($_POST['fName']) && !empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>bc<br>";
echo sfName_lName();
}
if(empty($_POST['filtercenter']) && !empty($_POST['fName']) && empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>b<br>";
echo sfName();
}
if(empty($_POST['filtercenter']) && empty($_POST['fName']) && !empty($_POST['lName']) && empty($_POST['filterfound']))
{
echo "<br>c<br>";
echo slName();
}
Hope this will help you...
Upvotes: 1