在JavaScript(jQuery/AJAX)中使用$ _GET,$ _ SERVER变量 [英] Use $_GET, $_SERVER variables in Javascript (jQuery / AJAX)

查看:92
本文介绍了在JavaScript(jQuery/AJAX)中使用$ _GET,$ _ SERVER变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,这是一个超级简单的jQuery/AJAX注释系统的第一步.第二步是将数据插入数据库的PHP.我需要页面的$ _GET ['variable']/$ _SERVER ['variable']存储到数据库中.我怎样才能在jQuery中得到这些.我不能只在PHP中说$spot = $_GET['spot'] or $url = $_SERVER['FILE_SCRIPTNAME'].它不会接.我必须通过jQuery/AJAX发送.我该怎么办?

$(document).ready(function() {
        $('#submit_comment').click(function() {
            var comment = $('#place_comment').val();
            // Somewhere here set the $_GET['variable'];
            $.ajax({
                type: 'POST',
                url: 'http://localhost/app/comment/comment.func.php',
                data: 'comment='+comment,
                success: function(data) {
                    $('#replies').html(data);
                    }
                });
            });
        });

解决方案

如果我了解您要正确执行的操作,则可以尝试使用类似方法在客户端javascript中使用服务器端PHP变量.

var comment = $('#place_comment').val();
var myVariable = '<?php echo $_GET['variable'] ?>';

So, heres a super simple jQuery / AJAX comment system step one. Step two is the PHP to insert the data into the DB. I need the $_GET['variable'] for the page / a $_SERVER['variable'] to store into the DB. How can I get these in the jquery. I can't just say $spot = $_GET['spot'] or $url = $_SERVER['FILE_SCRIPTNAME'] in the PHP. It won't pick it up. I has to be sent through the jQuery / AJAX. How can I do this?

$(document).ready(function() {
        $('#submit_comment').click(function() {
            var comment = $('#place_comment').val();
            // Somewhere here set the $_GET['variable'];
            $.ajax({
                type: 'POST',
                url: 'http://localhost/app/comment/comment.func.php',
                data: 'comment='+comment,
                success: function(data) {
                    $('#replies').html(data);
                    }
                });
            });
        });

解决方案

If I understand what you are trying to do correctly, you can try something like this to use server-side PHP variables in your client-side javascript.

var comment = $('#place_comment').val();
var myVariable = '<?php echo $_GET['variable'] ?>';

这篇关于在JavaScript(jQuery/AJAX)中使用$ _GET,$ _ SERVER变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆