doukan1258 2018-08-16 22:44
浏览 81
已采纳

PHP表单有多个问题,每个问题都有几个选项,可根据选择输出文本响应

I'm trying to create a simple PHP script that creates canned responses for different customer issues. I'd like a few different questions that can accept multiple answers via checkboxes or a dropdown. FYI I'm a beginner, so excuse my ignorance. I tried it with a switch statement, but ran into issues with needing to use more than one variable. Then I tried multiple elseif statements but couldn't get anything to evaluate past the first question. Each checkbox would essentially have a little excerpt that echos when it is the one selected. So with three questions, the output would actually create three snippets that all display in order. This way I could simply copy and paste the output. If anybody could help me out or even just point me in the right direction, I'd appreciate it. I've been doing a lot of googling and haven't gotten what I need.

Here is some unfinished test code I was messing with to see if I could figure it out. When I try this, only the part for colors works. The next part for food always goes to the evaluates as the 'else' portion, regardless of what's actually selected.

<?php
if(isset($_POST['submit'])) {
   $color_val = $_POST['Color'];
   $food_val = $_POST['food'];
   $drink_val = $_POST['drink'];


   if ($color_val == "Red") {
       echo "Red blurb";
       echo "<br>";
   }
   elseif ($color_val == "Green") {
       echo "Green blurb turb";
       echo "<br>";
   }
   elseif ($color_val == "Blue"){
       echo "True blue homey";
       echo "<br>";
   }

   else {
       echo "none selected";
       echo "<br>";
   }

   if ($food_val == "Wings") {
       echo "wingy wings";
       echo "<br>";
   }
   elseif ($food_val == "Pizza") {
       echo "pizza, pizza";
       echo "<br>";
   }
   else {
       echo "nothing selected";
       "<br>";
   }
?>


form action="#" method="post">
   <select name="Color">
       <option value="Red">Red</option>
       <option value="Green">Green</option>
       <option value="Blue">Blue</option>
   </select>
   <select name="food">
       <option value="wings">Wings</option>
       <option value="pizza">Pizza</option>
   </select>
   <select name = "drink">
       <option value="soda">Soda</option>
       <option value="water">Water</option>
       <option value="beer">Beer</option>
   </select>

   <input type="submit" name="submit" value="Get Selected Values" />
</form>
  • 写回答

2条回答 默认 最新

  • duanbeng8872 2018-08-16 23:03
    关注

    You are on the right track, but you need to either handle the variation in the values to the strings that you are comparing to or be consistent with the values that you are using in the html and your tests in php.

    $str = "Wings";
    $test = "wings";
    if ($str == $test){ //false
        echo "$str == $test";
    }
    if (strtolower($str) == $test){  //true
        echo "$str == $test";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?