dongsong1911 2012-10-12 14:20
浏览 23

将下拉值传递给PHP变量(不使用jQuery提交)

I am building a pizza ordering form that calculates form fields depends on user choices and returns the total. The first input will be the currency drop down, once user selects currency, it should pass value to PHP variable $currency (so that it will calculate the exchange rate) without the need to submit the form of course.

I have seen a lot of similar codes, but got confused, I know I should be using JavaScript / Ajax, but my knowledge is weak in that.

Can you please paste a simple code that will pass the $currency value to 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>
  • 写回答

3条回答 默认 最新

  • du0923 2012-10-12 14:24
    关注

    Php is run on the server before the HTML is returned to the browser. You cannot pass a value from an html form to a php variable for this reason.

    You can, however, submit a form to a php page for processing.

    <form action="myPhpPage.php">
    

    Then inside of myPhpPage.php you can use $_GET['variableName'] to retrieve the value

    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器