douao1959 2016-09-29 12:24
浏览 42
已采纳

php - 下拉onchange动作后重新加载当前页面

I have a dropdown element in my header file. The dropdown contains 2 currencies that I'd like users on my site to choose from:

<div class = "currencyswitcher">
 <span class = "currencylabel"><strong>Currency Switcher</strong> </span>
<form method="post" id="switchcurrency" name = "switchcurrency">
    <select name = "currency" id = "currency" >
        <option value = "USD"> USD </option>
        <option value = "KES"> KES </option>
    </select>

</form>
</div>

This is processed by an ajax script which posts the selected element to another file:

<script>
$(document).ready(function() {
    var url = "<?php echo file2.php;?>";
    $('#currency').change( function() {
       $.ajax({ 
           data: $('#switchcurrency').serialize(), 
           type: "POST",
           url: url, 
           success: function(response) { 
               alert(response);

           }
       });
    });  

}); 
</script>

now, in my file2.php I want to pass the value from my dropdown and append it to the url of the page i'm on i.e. if i'm on the page sitename.php/page1 then after selecting from the drop down, the page refreshes to sitename.php/page1?currency=USD and if i were on sitename.php/page2, upon selection, page refreshes to sitename.php/page2?currency=KES and so forth...

this is my file2.php(the file ajax is posting to)

if(isset($_POST["currency"]))
{
    $key = key($_POST);
    $value = $_POST["currency"];

    $params = array_merge($_GET, array($key => $value));
    $new_query_string = http_build_query($params);

    $url = (empty($_SERVER['HTTPS'])?"http://":"https://") . $_SERVER['REQUEST_URI'] . "?" . $new_query_string;
    var_dump($url);
}

From the above my dump returns the url of my header page i.e

var_dump($url); = mysiteurl/header.php?currency=USD

So here's my question: with my dropdown in the header(therefore called on all the pages in my site), how do I enforce it such that upon selecting an option, the current page I'm on refreshes having the selected option appended to the url?

  • 写回答

2条回答 默认 最新

  • douwu5428 2016-09-29 12:35
    关注

    Use the .done callback in your ajax to handle changing the window.location.href property (of which you can just append your currency parameter key/value pair):

    $(document).ready(function() {
        var url = "<?php echo file2.php;?>";
        $('#currency').change( function() {
           $.ajax({ 
               data: $('#currency').serialize(), 
               type: "POST",
               url: url, 
               success: function(response) { 
                   alert(response);
               }
           }).done(function() {
               window.location.href = window.location.href + "?currency="+$('#switchcurrency').val();
           });;
        });  
    }); 
    

    Further reading:

    http://api.jquery.com/jquery.ajax/

    https://developer.mozilla.org/en-US/docs/Web/API/Window/location

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

报告相同问题?

悬赏问题

  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建