简体   繁体   English

在Javascript中使用$ _GET,$ _ SERVER变量(jQuery / AJAX)

[英]Use $_GET, $_SERVER variables in Javascript (jQuery / AJAX)

So, heres a super simple jQuery / AJAX comment system step one. 因此,这是一个超级简单的jQuery / AJAX注释系统的第一步。 Step two is the PHP to insert the data into the DB. 第二步是将数据插入数据库的PHP。 I need the $_GET['variable'] for the page / a $_SERVER['variable'] to store into the DB. 我需要页面的$ _GET ['variable'] / $ _SERVER ['variable']存储到数据库中。 How can I get these in the jquery. 我怎样才能在jQuery中得到这些。 I can't just say $spot = $_GET['spot'] or $url = $_SERVER['FILE_SCRIPTNAME'] in the PHP. 我不能只说$spot = $_GET['spot'] or $url = $_SERVER['FILE_SCRIPTNAME']在PHP中。 It won't pick it up. 它不会接。 I has to be sent through the jQuery / AJAX. 我必须通过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. 如果我了解您要正确执行的操作,则可以尝试使用类似的方法在客户端JavaScript中使用服务器端PHP变量。

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

您不能像这样:javascript是客户端,而PHP $ SERVER数组是server-side

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM