doukan1258 2018-08-16 14: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 15: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部