首页 > 解决方案 > 如何使用ajax将javascript变量值传递给php变量以存储在codeighter的会话中

问题描述

我在 codeighther 工作,想将我的 javascript 变量值传递给 php,以便我可以将它存储在会话中,然后存储到数据库中。

以下是我到目前为止所尝试的。

<div id="wrapper">
    <button id="detect-button">Detect Device</button>
</div>

<script>
    $('#detect-button').ready(function() {

        var detector = new MobileDetect(window.navigator.userAgent)
        var test = document.write("Mobile: " + detector.mobile());

        //document.write( "Phone: " + detector.phone());
        //document.write( "Tablet: " + detector.tablet());
        // document.write( "OS: " + detector.os());
        // document.write( "userAgent: " + detector.userAgent());


    });

    $.ajax({
                type: 'Post',
                url: '<?php echo  $base_url ?>Home/index',
                data: ({
                    value: +detector.mobile()
                }),
                cache: false,
                success: function(data) {
                    $('#results').html(data);
                }
</script>

<p id="results"> </p>

标签: phpjquerysqlajax

解决方案


将检测器定义为全局变量。它目前的范围正在限制其使用


推荐阅读