2015-10-26 47 views
1

我在PHP上真的很糟糕。所以,是的,这段代码很简单。但如果可能的话,我宁愿让代码工作。谢谢。php错误 - 不知道如何修复它

目的PHP代码的

有技术两个领域,男性和女性的性别。我试图触发一个陈述,假设如果一个人选择0代表两个,告诉他们填写其中一个,否则,如果为空,填写它,否则,通过test_input函数测试它在底部。

我一直得到的是一致的说这样的错误:

PHP Parse error: syntax error, unexpected '[', expecting ')'在关于PHP代码的第一行。我非常喜欢这些代码,但无法弄清楚如何解决这个问题。

PHP代码

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域代码

 <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> 

测试功能

function test_input($data) { 
    $data = trim($data); 
    $data = stripslashes($data); 
    $data = htmlspecialchars($data); 
    return $data; 
} 
+0

'$ _POST([“female”])'不正确。您可以使用'{}'或'[]'来访问数组值。 http://php.net/manual/en/language.types.array.php – chris85

+0

@ chris85,它怎么不正确?我已经看过代码,直到我变蓝。可能是疲劳,但看不到它。 – user273072545345

+0

应该是'$ _POST [“female”])'看看额外的''('你有那里吗?它可能会更容易使用空的。 – chris85

回答

0

$_POST(["female"]) 

是不正确访问的POSTarrayfemale值。

卷曲{}或括号[]可用于访问数组。

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).

这应该是正确的:

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. 
} 

你也有不正确条件句。在控制结构上使用{}时,需要关闭控制块。您的elseif未关闭以前的if

+0

刚刚注意到错过了另外一个不正确的POST数组访问。 – chris85

+0

刚刚看到您的更新。现在测试它。 ftp慢! – user273072545345

+0

我仍然有问题,但我已经接受你的答案B/C它有点帮助,我希望能找出其余的。谢谢。 – user273072545345

0

你有令状10不正确的语法

试试这个

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"]); 
} 
+0

引发了这个错误:'PHP解析错误:语法错误,意外的T_ELSEIF' – user273072545345

+0

哦!我修正了语法。 –

+0

仍然引发了与您的代码的第三行相同的错误elseif部分。 – user273072545345

相关问题