I am having some difficulty getting the values of two text inputs:
<form action="search.php">
<input type="text" name="q1">
<input type="text" name="q2" >
<input type="submit" name="submit" value="Search" />
</form>
This is the search.php page:
$q1 = @$_GET['q1'];
$q2 = @$_GET['q2'];
if(isset($q1) && isset($q2)) {
$var= "$q1, $q2";
}
if(isset($q1) && empty($q2)) {
$var= "$q1";
}
When both q1 and q2 are filled out and sent, it works perfectly. However, when only the q1 input is filled out and sent (leaving q2 blank), it still creates $var using the the first if-statement — if(isset($q1) && isset($q2) — instead of the second one — if(isset($q1) && empty($q2). Why is this happening?
You’re setting
$q1and$q2soisset($q1)andisset($q2)will always return true. TryOr test for an empty value: