doutou6803 2015-01-15 05:11
浏览 38
已采纳

Javascript计算脚本以计算表单输入值,然后另存为page2.php的隐藏输入

So ive been spending the last week trying to figure out what im doing wrong.. i have tried to use several other scripts posted to other stack overflow users but can not seem to get anything to do what i need it to do, im still learning html, php and java script so everything is taking me a little while, so any help, advice or input is greatly appreciated.

So in my form i have several input fields that are selection fields, here is some for example:

                          </div>
                            <div class="form-field">
                              <label class="frm">*Approx Property Value</label>
                              <select name="fldAPV" title="Approx_Property_Value" id="fldAPV" class="form-action select-details">
                              <option value="100" selected="selected">Select value</option>
                              <option value="100">£0 - £100,000</option>
                              <option value="200">£100,001 - £200,000</option>
                              <option value="300">£200,001 - £300,000</option>
                              <option value="400">£300,001 - £400,000</option>
                              <option value="500">£400,001 - £500,000</option>
                              <option value="600">£500,001 - £600,000</option>
                              <option value="700">£600,001 - £700,000</option>
                              <option value="800">£700,001 - £800,000</option>
                              <option value="9000">£800,001 - £900,000</option>
                              <option value="1Million">£900,001 - £1,000,000</option>
                              </select>
                          </div>
                            <div class="form-field">
                              <label class="frm">*Number of Bedrooms</label>
                              <select name="fldNOBEDS2" title="Number_Of_Bedrooms" id="fldNOBEDS2" class="form-action select-details">
                                <option value="Not_Sure" selected="selected">I'm Not Sure</option>
                                <option value="1">1 Bedroom</option>
                                <option value="2">2 Bedrooms</option>
                                <option value="3">3 Bedrooms</option>
                                <option value="4">4 Bedrooms</option>
                                <option value="5">5 Bedrooms</option>
                                <option value="6">6 Bedrooms</option>
                                <option value="7">7 Bedrooms</option>
                                <option value="8+">8 Bedrooms +</option>
                              </select>
                            </div>                              
                            <div class="form-field">
                                <label class="frm">*Reason for Survey</label>   
                              <select name="fldRFS" title="Reason for Survey" id="fldRFS" class="form-action select-details">
     <option value="Not-Sure" selected="selected">Not Sure</option>
      <option value="Selling">Selling</option>
      <option value="Buying">Buying</option>
      <option value="Other-Reason">Other Reason</option>
  </select>

Now what i am trying to achieve is to have a javascript that will calculate the input value on each field the user selects his/hers answer.. My initial thoughts was to do it with each value="" having a default price value so for example the reason for survey fields, instead of saying "Not-Sure" or "Buying" they will say "50" or "100" to represent the cost of the service.

Is it possible to have a script that would understand code like this for example: Buying

The reason i say this is i am hoping the value's don't make things confusing, if not then i am happy to use the value="50" for example to determine what the overall cost of the service will be. I guess its the client basically putting together their own package.

The most important part of the script is to make the calculation hidden, so when clicking (View Quote) the form will submit the data either by $_GET or $_POST to page2.php where the client can view their quoted price.

I really hope i have not confused everyone! i see people post very small posts and they seem to get it on the head in one, i can not stress enough i am still learning and we all started somewhere :) This is for my own personal website and one hopes some kind experienced member can help me out, anything is much appreciated as i have spent 6 days trying to figure this out, Thanks!

King Regards

  • 写回答

2条回答 默认 最新

  • duanhuilao0787 2015-01-15 18:46
    关注

    PHP on page2.php:

    <?php 
    
    If(isset($_POST['submit'])){
        $Quote = $_POST['fldST'] + $_POST['fldAPV'] //... For all nummeric Fields
    
        Switch ($_POST['fldPropertyType']){ 
         /* Use the Switch Statement if you want to compare the same variable 
            (or expression) with   many different values, and execute a different piece of code  
            depending on which value it equals to.      */
    
            case 'Semi': 
               $Quote  = $Quote + 20; //Replace 20 wit you're Value
            case 'Terraced': 
               $Quote  = $Quote + 50;
            //... For all cases
        }
    
        //Other Code and Page Content
        echo $Quote;
    }
    else{
      //ERROR Page was accessed without filling the form in.
    }
    
    ?>
    

    I hope this gives you an good Idea how to do it. Not every Client has Javascript enabeld and you don't know about their Maschine Performance. ist the better way to calculate via PHP.

    Importend: NEVER echo/print/use POST/GET Variables direktly! Thats a big leck of Security. Always Filter your User Inputs to prevent Cross-Site-Scripting (XSS) http://phpsec.org/projects/guide/2.html http://resources.infosecinstitute.com/website-hacking-part-vi-input-validation-filtering-php/ http://phpsec.org/projects/guide/1.html#1.4.3

    <---------------------------------------------- Update ----------------------------------->

    When using the code above, Dreamweaver is saying that

    Switch ($_POST['fldPropertyType']){ 
    

    has a syntax error. Would this affect the script running before I add all the values or is it something simple to rectify?

    Thanks

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

报告相同问题?

悬赏问题

  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。