【问题标题】:create php form, show message depending user choice创建 php 表单,根据用户选择显示消息
【发布时间】:2011-11-20 17:47:40
【问题描述】:

我想创建一个带有复选框的 php 表单,如下所示:

< form method="post>
        <input type="checkbox" name="formpractise[]" value="A" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="B" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="C" />  <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="D" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="E" /> <br/>
        <br/>
        <input type="checkbox" name="formpractise[]" value="F" /> <br/>
        <br/>
        <input type="submit" name="formsubmit" value="Submit" />
</form>

当用户选择复选框时,我想根据用户选择打印一条消息。不是他做出的选择,而是取决于他的选择的信息。

【问题讨论】:

  • 我不知道确切的代码,但获取输入的值并使用if 语句为每一个打印出所需的消息。
  • 亲爱的@user1056615,你应该给那些花时间解决你的问题的人一些反馈。最后接受一个答案不会伤害... :-)

标签: php html forms checkbox


【解决方案1】:

如果您使用的是 jQuery 库,则 Javascript 将如下所示:

$(function(){
    // The messages
    var messages = {
        A  : "Message for A",
        B  : "Message for B",
        C  : "Message for C",
        D  : "Message for D",
        E  : "Message for E",
        F  : "Message for F"
    };
    $("input").click(function(){
        var key = $(this).val();
        // Only if the checkbox is checked
        if( $(this).is(":checked") )
        {
            // Alert the message
            alert(messages[key]);

            // Fill the HTML of the div with the id messageDIVID
            $("#messageDIVID").html(messages[key]);
        }
    });
});

如果您的 PHP 代码中包含所有消息。而且你有一个数组,你可以这样做

$messages = Array("A" => "Message for A", 
                  "B" => "Message for B", 
                  "C" => "Message for C", 
                  "D" => "Message for D", 
                  "E" => "Message for E", 
                  "F" => "Message for F");

echo "var messages = " . json_encode($messages) . ";";

【讨论】:

    【解决方案2】:

    您的复选框是一个数组,因此您需要考虑到这一点。您可以有多个有效的选择。不知道您的目标,但一个简单且简单的解决方案可能是:

    $choices = array(A,B,C,D,E,F)
    foreach ($choices as $choice) {
      if (in_array($choice, $_POST)) {
         // do something with your message
         // (append, edit, whatsoever)
      }
    }
    

    【讨论】:

      【解决方案3】:

      由于您使用复选框并且可以进行多项选择,因此我将使用二进制代码来帮助我为每个可能的组合选择不同的消息。 例如,如果用户选择框 A、D、E 和 F,您可以将其转换为二进制代码。 如果: F = 1 E = 2 D = 4 C = 8 乙 = 16 A = 32

      您将显示的消息是第 39 条消息(可能有 64 条不同的消息) A = 32 + D = 4 + E = 2 + F = 1

      32+4+2+1 = 39

      <form method="post">
              <label>
                <input type="checkbox" name="formpractise[]" value="32" /> Choice A
              </label> <br/>
              <label>
                <input type="checkbox" name="formpractise[]" value="16" /> Choice B
              </label> <br/>
              <label>
                <input type="checkbox" name="formpractise[]" value="8" /> Choice C
              </label> <br/>
              <label>
                <input type="checkbox" name="formpractise[]" value="4" /> Choice D
              </label> <br/>
              <label>
                <input type="checkbox" name="formpractise[]" value="2" /> Choice E
              </label> <br/>
              <label>
                <input type="checkbox" name="formpractise[]" value="1" /> Choice F
              </label> <br/>
              <input type="submit" value="Submit" />
      </form>
      <?
      if($_POST)
      {
          $message[1] = 'you will come into money';
          $message[2] = 'you will meet someone special';
          $message[3] = 'don\'t trust your neighbor';
          $message[4] = 'answers to your questions will come from SO';
          $message[5] = 'there\'s no need to read all of these';
          //...
          //...
          $message[39] = 'wow you selected lucky number 39';
          $message[40] = 'really, you\'re still reading';
      
          echo $message[array_sum($formpractise)];
      }
      ?>
      

      只需制作一个包含 64 条消息的数组,每个组合都会有不同的消息

      【讨论】:

      • 永远不会使用第 64 条消息。 32 + 16 + 8 + 4 + 2 + 1 = 63
      • 好点。 $message[0] 很可能需要在那里没有选择;-)
      【解决方案4】:

      您可以在一页中实现这一点:

      <?php
      $messages = array(      // Array of choices and messages
          'A' => 'Message A',
          'B' => 'Message B',
          'C' => 'Message C',
          'D' => 'Message D',
          'E' => 'Message E',
          'F' => 'Message F'
      );
      
      if($_POST['submit'] === 'Submit') {  // If the form is submitted
          // If a choice was made and it is in the messages array
          if(isset($_POST['choice']) && array_key_exists($_POST['choice'], $messages)) {
              die($messages[$_POST['choice']]);  // Display the message and end execution
          } else {
              die('Unknown Message');  // Otherwise, display and error and end execution
          }
      }
      ?>
      
      <form action="" method="POST">
          <p><input type="radio" name="choice" value="A" /> A</p>
          <p><input type="radio" name="choice" value="B" /> B</p>
          <p><input type="radio" name="choice" value="C" /> C</p>
          <p><input type="radio" name="choice" value="D" /> D</p>
          <p><input type="radio" name="choice" value="E" /> E</p>
          <p><input type="radio" name="choice" value="F" /> F</p>
          <p><input type="submit" name="submit" value="Submit" /></p>
      </form>
      

      我也相信在这种情况下您正在寻找单选按钮而不是复选框。复选框允许在一组框中进行多项检查,其中单选按钮仅允许从组中选择一个。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 1970-01-01
        • 2018-12-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多