【发布时间】:2012-10-13 05:49:29
【问题描述】:
我正在构建一个披萨订购表单,该表单根据用户选择计算表单字段并返回总数。 第一个输入将是货币下拉菜单,一旦用户选择货币,它应该将值传递给 PHP 变量 $currency(以便计算汇率) 当然不需要提交表格。
我看过很多类似的代码,但很困惑,我知道我应该使用 JavaScript / Ajax,但我的知识很薄弱。
您能否粘贴一个将 $currency 值传递给 PHP 的简单代码?
<html>
<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
jQuery(function($){
$("#currency").change(function(){
var selected_value = $(this).val();
/*AJAX CODE HERE TO PASS VALUE OF DROP DOWN TO PHP*/
$.post("CurrencyCalculator.php", $("#currency").serialize()); //this is the problematic part: it needs to pass the $currency variable
})
})
</script>
</head>
<body>
Select any one:
<select id="currency" name="currency" onchange="selectDropdown()">
<option value="EUR" >Euro</option>
<option value="USD" >US Dollar</option>
<option value="GBP" >British Pound</option>
</select>
</body>
</html>
【问题讨论】:
-
这个能赚多少钱?你检查了吗php.net/manual/en/function.money-format.php
-
您阅读过 jQuery 文档吗? api.jquery.com/jQuery.post 将是一个不错的起点。
-
我知道答案在这里api.jquery.com/jQuery.post,但我仍然无法弄清楚:)
-
你在哪一部分遇到了问题?
-
@NullPointer 你说对了一半 :) 它不是为了披萨 :P (但实际上,例如在匈牙利,你用匈牙利福林支付,而你也应该接受欧元,因为它在欧盟。
标签: php javascript jquery html ajax