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 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法