【问题标题】:PHP When 2 or more conditions are true (if statement)PHP 当 2 个或多个条件为真时(if 语句)
【发布时间】:2014-07-08 22:43:14
【问题描述】:

所以我是 php 新手,并尝试练习用户可以在哪里选择他们想要的服务类型。他们可以选择不止一项服务,这就是我使用复选框而不是单选按钮的原因。如果他们只选择一项服务(服务 A、服务 B 或服务 C),我可以在文本框中得到正确的返回。当我尝试查看用户是否选中了 2 个框(服务 A 和服务 B)时,返回就好像用户只选择了服务 B。

这是我所有的代码:

<form action="" method="post">
<table>
<tr>
<td>
<input type="checkbox" name="service" value="a" />Service A</td>
</tr>
<tr>
<td>
<input type="checkbox" name="service" value="b" />Service B</td>
</tr>
<tr>
<td>
<input type="checkbox" name="service" value="c" />Service C</td>
</tr>
</table>
<input type="submit" name="submit" value="submit">
<?php
if(isset($_POST['submit']))
{
$a = $_POST['service'];


if($a == "a")
{
echo "<input type='text' name='txt' value='$1,000'/>";

}

if($a == "b")
{
echo "<input type='text' name='txt' value='$20,000'/>";

}
if($a == "c")
{
echo "<input type='text' name='txt' value='$300,000'/>";

}
if(($a == "a" and $a == "b"))
{
echo "<input type='text' name='txt' value='$21,000'/>";

}

}
?>

我也尝试过 && 而不是 and 仍然得到相同的结果。

我提前感谢您的帮助。

已编辑以显示整个代码。

【问题讨论】:

  • 嗯...我不是有意冒犯,但$a == 'a' &amp;&amp; $a == 'b' 永远不可能是真的,因为'a' != 'b'
  • 如果 $a == 'a'$a == 'b',此代码将产生两个同名的文本字段,如前面代码中的 if 语句 if($a == 'a')if($a == 'b')

标签: php if-statement conditional-statements multiple-conditions


【解决方案1】:

你的复选框应该这样定义:

&lt;input type="checkbox" name="service<b><i>[]</i></b>" value="a" /&gt;

请注意那里的[]

现在,在您的 PHP 端,您可以这样做:

$a = array_flip($_POST['service']);
if( isset($a['a'])) { /* a was checked */ }
if( isset($a['b'])) { /* b was checked */ }
if( isset($a['a'],$a['b'])) { /* a and b were checked */ }

或者,更可靠的是,您可能希望这样做:

$total = 0;
if( isset($a['a'])) $total += 1000;
if( isset($a['b'])) $total += 20000;
if( isset($a['c'])) $total += 300000;
echo '<input type="text" name="txt" value="$'.number_format($total).'" />';

【讨论】:

    【解决方案2】:

    你有两个主要问题,你的第一个是你的输入需要是一个复选框数组,像这样:

    <input type="checkbox" name="service[]" value="a">
    <input type="checkbox" name="service[]" value="b">
    <input type="checkbox" name="service[]" value="c">
    

    然后我们可以更改 PHP 以使用复选框数组来代替:

    if(isset($_POST['submit'])) {
      $services = $_POST['service']; // a should now be an array if you're using checkboxes.
      $value = 0; // keep track of all the service values
      $service_list = array(); // keep track of the services ordered
    
      if(in_array('a', $services)) { // see if a is in the array
        $value += 1000;
        $service_list[] = 'a';
      }
      if(in_array('b', $services)) { // see if b is in the array
        $value += 20000;
        $service_list[] = 'b';
      }
      if(in_array('c', $services)) { // see if c is in the array
        $value += 300000;
        $service_list[] = 'c';
      }
    }
    
    echo "You chose the service(s) " . implode(', ', $service_list) . " for a total of $" . number_format($value);
    echo "<input type=\"text\" name=\"txt\" value=\"$". number_format($value) ."\" />";
    

    【讨论】:

    • 它正在工作,但由于某种原因,我收到一条警告说我的用户将能够看到,我想修复它并找出该怎么做,这样我就不会再遇到这种情况了。这是它所说的: 注意:未定义的变量:第 43 行 C:\xampp\htdocs\phpPracCheckBox\testphps.php 中的 service_list 警告:implode():在 C:\xampp\htdocs\phpPracCheckBox\testphps.php 中传递的参数无效第 43 行注意:未定义变量:第 43 行 C:\xampp\htdocs\phpPracCheckBox\testphps.php 中的值
    • 这是第 43 行的代码: 这是第 43 行的代码: echo "You choose the service(s) " 。内爆(',',$service_list)。 " 总共 $" 。 number_format($value);
    • 这是$_POST['submit']) 未设置的结果。您应该只在 POST 之后回显最后两行。
    • 对不起,我不明白怎么没设置好?
    • $service_list = array(); 设置在第一个 if 语句中。如果它永远不会进入那个 if 语句(即,如果它不是 POST),那么 $service_list 将永远不会被设置,你会得到这个错误。解决此问题的最简单方法是将其和 $value = 0; 行移到 if 语句上方。
    【解决方案3】:

    只有单选按钮可以具有相同的名称。复选框必须具有唯一值。当发布键值对时,浏览器收到的最后一个键值对将覆盖之前的其他键值对。

    <input type="checkbox" name="A" value="1" />Service A
    <input type="checkbox" name="B" value="2" />Service B
    <input type="checkbox" name="C" value="3" />Service C
    
    $a = intval($_POST['A'])  // if checkbox was not checked
    $b = intval($_POST['B'])  //   intval will make it zero.
    $c = intval($_POST['C'])
    
    $values = array(0,1000,20000,30000);
    $descriptions = array('','Service A&#x2003;','Service B&#x2003;','Service C&#x2003;');
    
    
    $value = number_format($values[$a] + $values[$b] + $values[$c]);
    $description = $descriptions[$a] . $descriptions[$b]. $descriptions[$c] . '$' . number_format($value);
    
    echo "<input type=\"text\" name=\"txt\" value=\"$value\" />$description<br/>";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      • 1970-01-01
      • 1970-01-01
      • 2013-12-17
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多