I'm really bad at PHP. So, yes, this code is simplistic. But I'd rather get this code working if possible. Thank you.
Purpose of PHP Code
There are technically two fields, male and female genders. I am trying to trigger a statement, assuming that if a person picks 0 for both, to tell them fill one of them, otherwise, if empty, to fill it in, otherwise, to test it via test_input
function which is at bottom.
What I keep getting is an error that consistently says this:
PHP Parse error: syntax error, unexpected '[', expecting ')'
in regards to the first line of the PHP code. I've raked over the code so much but can't figure out how to fix it.
PHP Code
if ((($_POST["male"]) === "") && ($_POST(["female"]) === "")) {
$quantityErr = "pls fill in one of the genders";
elseif (empty($_POST(["male"]))
$maleErr = "# of people (gender male) required";
else
$male = test_input($_POST["male"]);
}
HTML Field Code
<div class="field">
<label>* Number of People</label>
<select class="ui dropdown" name="male">
<option value="">Gender Male</option>
<option <?php if ($male === 0 ) echo 'selected' ; ?> value="0">0</option>
<option <?php if ($male == 1 ) echo 'selected' ; ?> value="1">1</option>
<option <?php if ($male == 2 ) echo 'selected' ; ?> value="2">2</option>
<option <?php if ($male == 3 ) echo 'selected' ; ?> value="3">3</option>
<option <?php if ($male == 4 ) echo 'selected' ; ?> value="4">4</option>
<option <?php if ($male == 5 ) echo 'selected' ; ?> value="5">5</option>
<option <?php if ($male == 6 ) echo 'selected' ; ?> value="6">6</option>
<option <?php if ($male == 7 ) echo 'selected' ; ?> value="7">7</option>
<option <?php if ($male == 8 ) echo 'selected' ; ?> value="8">8</option>
<option <?php if ($male == 9 ) echo 'selected' ; ?> value="9">9</option>
<option <?php if ($male == 10 ) echo 'selected' ; ?> value="10">10</option>
</select>
<?php if(isset($maleErr)) print ('<span class="error">* ' . $maleErr . '</span>'); ?>
<?php if(isset($quantityErr)) print ('<span class="error">* ' . $quantityErr . '</span>'); ?>
</div>
Test Function
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
This
$_POST(["female"])
is incorrect to access the female
value of the POST
array.
Curlys {}
or brackets []
can be used to access arrays.
Both square brackets and curly braces can be used interchangeably for accessing array elements (e.g. $array[42] and $array{42} will both do the same thing in the example above).
This should be correct:
if (empty($_POST["male"]) && empty($_POST["female"])) {
$quantityErr = "pls fill in one of the genders";
} elseif (!empty($_POST["male"]) && $_POST["male"] > 0) {
$male = test_input($_POST["male"]);
} elseif (empty($_POST["male"])) {
//male is not set or equal to 0; if male is 0 then female >= 1 and set. logic doesn't have any female checks so unclear currently how you want to handle that
$maleErr = "# of people (gender male) required"; // maybe add message that it is required to be greater than 1.
}
You also had incorrect conditionals. When using {}
s on control structures you need to close the control block. Your elseif
wasn't closing the previous if
.
you have written incorrect syntax
try this
if ((($_POST["male"]) === "") && ($_POST["female"] === "")) {
$quantityErr = "pls fill in one of the genders";
elseif(empty($_POST["male"]))
$maleErr = "# of people (gender male) required";
else
$male = test_input($_POST["male"]);
}