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条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效