开发者

Passing query strings with jQuery

I've been googling for two hours now, can't find开发者_开发百科 the answer. Hope you guys can help me out.

The following is part of a refresh script in jQuery. I'm using this code in index.php:

<script type="text/javascript">
jQuery.noConflict();

// UPDATE TYPES
function typeUpdate() {
    jQuery.get("script.php"), function(data) {
        jQuery('#heroTypes').html(data);
    });
    window.setTimeout("typeUpdate();", 500);
};
</script>

That script.php contains a SQL command that gets information and updates a div in index.php. It works perfectly, but the thing is that now I need to pass a query string to that file. I tried doing like this but it won't work:

jQuery.get("script.php?s=<? $querySetup ?>"), function(data) {

$querySetup is the php variable that gets the query string. If I try to send it manually (for instance "script.php?s=53") it doesn't work either. Would really appreciate help here, is there any way to achieve this? Thanks in advance!


Try this (assuming $querySetup has been validated as safe to use)

jQuery(function($) {
    var heroTypes = $('#heroTypes');
    var interval = window.setInterval(function() {
        $.get('script.php', { s: "<?php echo $querySetup ?>" }, function(data) {
            heroTypes.html(data);
        });
    }, 500);
});

I hope your web server and database like getting hit twice a second.

I'm also concerned about this $querySetup variable. You realise in the context of index.php, this will not change.


Apart from your JavaScript syntax errors, you are not printing the PHP variable:

<? $querySetup ?>

You probably want one of these:

<? echo $querySetup ?>
<?=$querySetup ?>


<?= $querySetup ?> instead of <? $querySetup ?>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜