Javascript Calculation脚本来计算表单输入值,然后保存为page2.php的隐藏输入 [英] Javascript Calculation script to calculate form input value's then save as hidden input for page2.php
问题描述
因此,在我的表单中,我有几个输入字段是选择字段,例如:
< / div>
< div class =form-field>
< label class =frm> *近似属性值< / label>
< option value =100selected =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> *卧室数量< / label>
< option value =Not_Sureselected =selected>我不确定< / option>
< option value =1> 1间卧室< / option>
< option value =2> 2间卧室< / option>
< option value =3> 3间卧室< / option>
< option value =4> 4间卧室< / option>
< option value =5> 5间卧室< / option>
< option value =6> 6间卧室< / option>
< option value =7> 7间卧室< / option>
< option value =8 +> 8间卧室+< / option>
< / select>
< / div>
< div class =form-field>
< label class =frm> *调查原因< / label>
< option value =Not-Sureselected =selected>不确定< / option>
< option value =Selling> Selling< / option>
<选项值=购买>购买< / option>
< option value =其他原因>其他原因< / option>
< / select>
现在我想实现的是有一个javascript来计算每个字段的输入值用户选择他/她的答案..我最初的想法是做与每个值=有一个默认的价格值,所以例如调查领域的原因,而不是说不确定或购买他们会说50或100来表示服务的成本。
是否有可以理解如下代码的脚本:
购买
我之所以这么说,是因为我希望价值不会令人困惑,如果不是,那么我很乐意使用value =50来代替例如确定服务的总体成本。我猜它的客户基本上是把自己的软件包放在一起。
这个脚本最重要的部分是隐藏计算,所以当点击(View Quote)表单将数据由$ _GET或$ _POST提交给page2.php,客户可以在其中查看他们的报价。
我真的很希望我没有让所有人感到困惑!我看到人们张贴非常小的帖子,他们似乎把它在头上,我不能强调我仍然在学习,我们都开始在某个地方:)这是为我自己的个人网站,并希望有一种有经验的会员可以帮助我,任何东西都非常感谢,因为我已经花了6天的时间来解决这个问题,谢谢!
国王问候
<?php $ b =h2_lin>解决方案
$ b如果(isset($ _ POST ['submit'])){
$ Quote = $ _POST ['fldST'] + $ _POST ['fldAPV'] // ...对于所有数字字段
Switch($ _POST ['fldPropertyType']){
/ *如果您想将同一个变量
(或表达式)与许多不同的值进行比较,请使用Switch语句,并执行一段不同的代码
,这取决于它与哪个值相等。 * /
case'Semi':
$ Quote = $ Quote + 20; //替换20就是你的价值
案例'Terraced':
$ Quote = $ Quote + 50;
// ...对于所有情况
}
//其他代码和页面内容
echo $ Quote;
}
else {
//错误页面无需填写表单即可访问。
}
?>
我希望这给你一个很好的想法如何去做。并非每个客户都拥有Javascript enabeld,并且您不了解他们的Maschine Performance。是更好的方式来通过PHP来计算。
Importend:从不使用echo / print /使用POST / GET变量direktly!这是安全的一大漏洞。始终过滤您的用户输入以防止跨站点脚本(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
< -------------------------------------- --------更新----------------------------------->
使用上面的代码时,Dreamweaver会说
$ p $
Switch($ _POST [ fldPropertyType']){
有语法错误。在添加所有值之前,这会影响脚本运行吗?还是很容易纠正?
谢谢
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
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
这篇关于Javascript Calculation脚本来计算表单输入值,然后保存为page2.php的隐藏输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!