duanlvxing7707 2013-09-01 12:49
浏览 53
已采纳

如何将此javascript的结果转换为php变量?

There is a nifty piece of code that lets users start typing a place name, then offers suggested locations as you type.

Clicking on the desired location then places the location name into a div box. How can i get that selected value as a variable $location?

 <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css" />
<style>
.ui-autocomplete-loading {
background: white url('images/ui-anim_basic_16x16.gif') right center no-repeat;
}
#city { width: 25em; }
</style>

<script>
$(function () {
    function log(message) {
        $("<div>").text(message).prependTo("#log");
        $("#log").scrollTop(0);
    }
    $("#city").autocomplete({
        source: function (request, response) {
            $.ajax({
                url: "http://ws.geonames.org/searchJSON",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
                success: function (data) {
                    response($.map(data.geonames, function (item) {
                        return {
                            label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                            value: item.name
                        }
                    }));
                }
            });
        },
        minLength: 2,
        select: function (event, ui) {
            log(ui.item ?
                "Selected: " + ui.item.label :
                "Nothing selected, input was " + this.value);
        },
        open: function () {
            $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
        },
        close: function () {
            $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
        }
    });
});
</script>

Divs:

<div class="ui-widget">
<label for="city">Your city: </label>
<input id="city" />
<div class="ui-widget" style="margin-top: 2em; font-family: Arial;">
Result:
<div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
</div>
  • 写回答

1条回答 默认 最新

  • doukan1258 2013-09-01 13:23
    关注

    If you want to use this nifty piece of code for filling a form, what user can submit and php can work with on serverside, then you have to make it a form and give city field an appropriate name:

    <form method="post">
    
    <div class="ui-widget">
    <label for="city">Your city: </label>
    <input id="city" name="city" />
    <div class="ui-widget" style="margin-top: 2em; font-family: Arial;">
    Result:
    <div id="log" style="height: 200px; width: 300px; overflow: auto;" class="ui-widget-content"></div>
    </div>
    
    <input type="submit" value="ok" />
    
    </form>
    
    <?php
    if (!empty($_POST['city'])){
        print('<div>Latest submitted city was: '.$_POST['city'].'</div>');
    }
    ?>
    

    Put this amended stuff (together with javascript) into a .php file and point your browser to this.

    EDIT:

    Alternative way would be to use cookie :)

    Before success() return in your code set a cookie:

    success: function (data) {
        response($.map(data.geonames, function (item) {
            document.cookie = 'location=' + encodeURIComponent(item.name) + '; path=/';
            return {
                label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                value: item.name
            }
        }));
    }
    

    In your PHP you can read this as:

    $location = $_COOKIE['location'];
    print($location);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题