复选框条件会导致另一页

时间:2013-11-22 10:04:17

标签: php

我想在复选框上创建条件。

如果你选择1,那么它将进入他的下一页 如果您选择2和3,那将是一个感谢页面

可以帮助我

    <input type="checkbox" name="number[]" id="" value="1">1 <br />
<input type="checkbox" name="number[]" id="" value="2">2 <br />
<input type="checkbox" name="number[]" id="" value="3">3

<?php
if (
        (
            ($number == '1') && ($number == '2')
        ) 
            || 
        (
            ($number == '1') && ($number == '3')
        ) 
            || 
        (
            ($number == '1') && ($number == '2') && ($number == '3')
        )
    )

      {
        header("Location: next.php"); /* Redirect browser */
      } else {
        header("location: thank-you.php");
        }

2 个答案:

答案 0 :(得分:1)

看起来你正试图通过寄存器全局变量访问GET / POST变量 - 请不要这样做。使用$_GET$_POST(取决于您从表单传递数据的方式)。此外,选中的值将存储在数组中,因此您不能只将数组与值进行比较。使用in_array()函数(http://php.net/manual/en/function.in-array.php)。

实施例: if (in_array('1', $_POST['number'])) { ... }

答案 1 :(得分:1)

您可以简单地处理表单的结果并确定要采取的操作。例如,您的表单可能是:

<form method="post" action="processform.php">
    <input type="checkbox" name="number[]" id="" value="1">1 <br />
    <input type="checkbox" name="number[]" id="" value="2">2 <br />
    <input type="checkbox" name="number[]" id="" value="3">3 <br />
</form>

然后在processform.php文件中,查看选择了哪些值并执行相应的操作。例如,将所有选择放在胶合字符串中,然后使用开关来处理:

<?php
// Set the value to an empty string initially (meaning nothing was selected)
$value = '';

// Determine the selected values and "glue" them together as a numeric string
foreach ($_POST['number'] as $number) {
   $value .= $number;
}

// Now, take the appropriate action
switch ($value) {
    case '1':
        // Only checkbox 1 was selected
        header('Location: page-1.php');
        break;
    case '12':
        // Checkbox 1 and 2 were selected
        header('Location: page-12.php');
        break;
    case '13':
        // Checkbox 1 and 3 were selected
        header('Location: page-13.php');
        break;
    // Keep doing this for all the combinations you want
    // If none of these are triggered, the default action below will be...
    default:
        // Either nothing was selected, or something not listed above
        header('Location: try-again.php');
}