douzhi3105 2014-10-05 12:28
浏览 25
已采纳

报价表 - 从PHP表单选择中回显两个值

I am trying to create a quotation form. This will be email to myself and the person who has filled in the form. I want to echo the selected value (I currently have this working) & assign a integer value to the selection. I then want to add the integers up to give a total. In my example below I have just included one selection but I will have quite a few of them in the form. Any help would be great!

<label>What time would you like to start?
    <select required name="drop_off_time" id="drop_off_time-select" value="<?php if(isset($_POST['drop_off_time']))  echo $_POST['drop_off_time'];?>">
        <option value="0"></option>
        <option value="8:00am">8:00am</option>
        <option value="8:30am">8:30am</option>
        <option value="9:00am">9:00am</option>
    </select>
</label>


if(isset($_REQUEST['drop_off_time']) && $_REQUEST['drop_off_time'] == '0') { 
echo 'Please select a time.'; 
} 
$drop_off_time = $_POST['drop_off_time'];


$emailTo = 'me@gmail.com';
$subject = 'Fees Form Submission from '.$name;
$sendCopy = trim($_POST['sendCopy']);
$body = "Name: $name 

Email: $email 

Comments: $comments 

Drop Off Time: $drop_off_time 

Total: £$total";
$headers = 'From: My Site <'.$emailTo.'>' . "
" . 'Reply-To: ' . $email;

mail($emailTo, $subject, $body, $headers);

This all works fine. My problem is that I want to assign an integer to the selection. I've done this using a switch but it keeps printing out the selected value not the new value I have assigned. The code for this is below.

$drop_off_time_price = 0;
switch ($drop_off_time_price) {
case 0;
    echo 0;
    break;
case "8:00am":
  echo 100;
  break;
case "8:30am":
  echo 10000;
  break;
case "9:00am":
  echo 100000;
  break; 
}

//Getting the total
$total = $drop_off_time_price; 

This is what the email looks like

Name: Frank

Email: me@mail.com

Comments: I get the selected value but no the total....

Drop Off Time: 8:00am

Total: £0

  • 写回答

3条回答 默认 最新

  • douweibeng5219 2014-10-05 12:42
    关注

    You have a few problems in your logic:

    1. You should be checking against $drop_off_time in your switch statements. So it should be:

      switch ($drop_off_time) {
      
    2. as @FloatingRock has mentioned, case 0; should be:

      case 0:
      
    3. instead of echo 0 you should assign that value to $drop_off_time_price

      $drop_off_time_price = 0;
      

    So your full example should be something like:

        $drop_off_time_price = 0;
        switch ($drop_off_time) {
            case "8:00am":
               $drop_off_time_price = 100;
               break;
            case "8:30am":
               $drop_off_time_price = 10000;
               break;
            case "9:00am":
               $drop_off_time_price = 100000;
               break; 
        }
    

    Note: You can also remove the check for 0 as in my example since you are already setting the default value for 0 to be 0 price.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错