HTML选择仅将第一个值传递给PHP

时间:2017-07-10 21:34:09

标签: php html html-select

我正在编写一个在HTML中使用<select>的程序。当我提交表单并执行PHP脚本时,我正在使用$_POST从HTML页面中获取所选选项。但是,每次只传递“1”。我尝试过选择不同的值,但总是选择1.这是代码:

HTML:

<form id="form_22942" class="appnitro" method="post" action="../process/process.php">
  <div class="form_description"></div>
  </li>
  <li class="section_break">
    <h3>Deployment Constraints</h3>
    <p></p>
  </li>
  <li id="li_11">
    <label class="description" for="element_11">Corporate Policies and Procedures: </label>
    <div>
      <select class="element select medium" id="element_11" name="element_11"> 
        <option value="" selected="selected"></option>
        <option value="1" >Flexible</option>
        <option value="2" >Fixed</option>
      </select>
    </div>
</form>

PHP(process.php):

$corp_policy = $_POST['element_11'];
$entityname2 = "corp_policy";

if ($corp_policy = '1') {
    echo "<center>Corporate policies and procedures are flexible.<center>";
    $corp_policy_text = "Flexible";
}
else if ($corp_policy = '2') {
    echo "<center>Corporate policies and procedures are fixed.<center>";
    $corp_policy_text = "Fixed";
}

3 个答案:

答案 0 :(得分:3)

您正在为$ corp_policy分配1而不是比较两个操作数。

使用==或===

检查文档以获取更多信息:http://php.net/manual/en/language.operators.comparison.php

答案 1 :(得分:0)

你应该使用这些:(==

if ($corp_policy == '1') {
    echo "<center>Corporate policies and procedures are flexible.<center>";
    $corp_policy_text = "Flexible";
}
else if ($corp_policy == '2') {
    echo "<center>Corporate policies and procedures are fixed.<center>";
    $corp_policy_text = "Fixed";
}

而不是:{=

if ($corp_policy = '1') {
    echo "<center>Corporate policies and procedures are flexible.<center>";
    $corp_policy_text = "Flexible";
}
else if ($corp_policy = '2') {
    echo "<center>Corporate policies and procedures are fixed.<center>";
    $corp_policy_text = "Fixed";
}

答案 2 :(得分:0)

<select class="element select medium" id="element_11" name="element_11"> 
<option disabled selected > -- select an option -- </option>
        <option value="" selected="selected"></option>
        <option value="1" >Flexible</option>
        <option value="2" >Fixed</option>
 </select>

它对我有用