I'm ajaxing over to this php file.
$a = 'old';
$b = 'new';
if ($_POST['info-type'] == $a || $b)
{
$info = $_POST['info-type'];
$query = "SELECT * FROM `tld` WHERE type = '".$var."'";
}
$query = "SELECT * FROM `tld` ";
$result = mysqli_query($link,$query);
while($row = mysqli_fetch_assoc($result))
{
echo '<div>'.$row['something'].'</div>';
}
The data posted is either 'all'
'new'
or 'old'
.
If I send the data as either new
or old
, the script works and outputs as expected. If the posted data is neither new or old but all instead, it fails and don't show any errors or respond anything back (I've monitored via dev tools as well).
So, I tried this:
if ($_POST['info-type'] == $a || $b)
{
$info = $_POST['info-type'];
$var = "SELECT * FROM `tld` WHERE type = '".$var."'";
} elseif ($_POST['info-type'] == 'all')
{
$query = "SELECT * FROM `tld` ";
}
But the script still fails. If i fully remove the IF
statements and use the query without the WHERE
clause like it is after the elseif, it works?
You need to compare $_POST['info-type']
with both $a
and $b
:
if ($_POST['info-type'] == $a || $_POST['info-type'] == $b)
The problem with what you have is that $_POST['info-type'] == $a || $b
sees if $_POST['info-type'] == $a
is true OR if $b
is true.
Since $b
is true (it is non-empty string), the if
condition will always be true.
If $_POST['info-type']
can only be all
new
or old
, then the following will be more elegant:
if ($_POST['info-type'] == 'all')
{
$query = "SELECT * FROM `tld` ";
}
else // info type will be old or new
{
$info = $_POST['info-type'];
$query = "SELECT * FROM `tld` WHERE type = '".$info."'";
}
This statement is very odd:
if ($_POST['info-type'] == $a || $b) {
You probably intended:
if ($_POST['info-type'] === $a || $_POST['info-type'] === $b) {
Also, in this case ===
is good since the type of both variables is string
.
$_POST['info-type'] == $a || $b
will always be true if $b
is truethy (which it is).