dongshuo6503 2017-04-17 18:24
浏览 42
已采纳

将jQuery生成的值传递给PHP Mail

This seems simple, but I can't find a solution. I created an HTML form that uses a little bit of jQuery to generate a value (total price) based on user selections in dropdown menus. I'm trying to get that generated value to show up in the email that is being sent via PHP Mail. All other values from my form are showing up, but I can't seem to get that value at all. I'm using the POST method.

Here's the HTML (this is just a small portion showing the part I need. This displays the value to the screen, based on what the user chooses in the drop down menu):

<form name="quote" id="price-quote" action="mail.php" method="post">    
  <label class="control-label" for="carpet_cleaning">Bedrooms</label><br>
    <select class="form-control-1" id="carpet_cleaning" name ="bedrooms_selection">
         <option value="0">0</option>
         <option value="1">1</option>
         <option value="2">2</option>
    </select>

<!--displays the total price to the screen-->
 <div class="total-price">
    <h2>Total Price: <span id="output1" name="total_price"></h2>
 </div>
</form>

jQuery

<script>
$("#carpet_cleaning").change(function (){
  var bedrooms = $("#carpet_cleaning").val();
  var total = (bedrooms * 10);
  var totalPrice = "$" + total;
  $("#output1").text(totalPrice).fadeIn();
});
</script>

Super simple JS Fiddle for a better visual https://jsfiddle.net/b7tyx7uk/

So here's the PHP code I have that will send the email

<?php 
if (!empty($_POST['bedrooms_selection'])){
  $services[]=' Bedrooms: '.$_POST['bedrooms_selection'];
}
$totalPrice = $_POST['total_price'];
 if($services){
 $email_subject = "Price Quote";
 $to = "jondoe@example.com";
 $email_body="Services Needed: 
" .implode("
",$services) . "

Total Price: $totalPrice";
 $headers = "From: janedoe@example.com";
 mail($to, $email_subject, $email_body, $headers);
 header("Location: http://example.com");
}
?>

It's working with all user inputs (not displayed in this post), but I can't display the total price that is generated from the jQuery. Am I overlooking something? I thought I could just give it a name and then use that name to create a new variable in the PHP, but it's not pulling that value at all. Thanks for any help!

  • 写回答

1条回答 默认 最新

  • doucheng3811 2017-04-17 18:29
    关注

    Forms don't send the contents of DIVs to the server, only the values of inputs. Add a hidden input.

    <form name="quote" id="price-quote" action="mail.php" method="post">    
      <label class="control-label" for="carpet_cleaning">Bedrooms</label><br>
        <select class="form-control-1" id="carpet_cleaning" name ="bedrooms_selection">
             <option value="0">0</option>
             <option value="1">1</option>
             <option value="2">2</option>
        </select>
    
    <!--displays the total price to the screen-->
     <div class="total-price">
        <h2>Total Price: <span id="output1" name="total_price"></h2>
     </div>
     <input type="hidden" id="total_price">
    </form>
    
    <script>
    $("#carpet_cleaning").change(function (){
      var bedrooms = $("#carpet_cleaning").val();
      var total = (bedrooms * 10);
      var totalPrice = "$" + total;
      $("#output1").text(totalPrice).fadeIn();
      $("#total_price").val(totalPrice);
    });
    </script>
    

    That said, you probably should calculate the price on the server. Otherwise, the user could manipulate the price using the browser console.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考