1

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!

Álvaro González
147k45 gold badges282 silver badges378 bronze badges
asked Aug 11, 2011 at 6:49
8
  • 3
    you have a redundant round bracket ")" in jQuery.get("script.php") Commented Aug 11, 2011 at 6:51
  • What about such code jQuery('#heroTypes').load("script.php?s=53"); Does it work? Commented Aug 11, 2011 at 6:54
  • Does the $querySetup catch the complete query string or just a get value ($_GET['s'])? Try urldecode as well. Commented Aug 11, 2011 at 6:55
  • The extra bracket is just a mistake by me when pasting this post. Commented Aug 11, 2011 at 7:10
  • @Shadow Wizard, I don't think so. What line should I replace it with? Commented Aug 11, 2011 at 7:11

3 Answers 3

0

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.

answered Aug 11, 2011 at 6:59

8 Comments

Thanks for the answer! Can't get it to work though. And by the way, I'm just using that interval for the moment.
You'll have to define "can't get it to work". Are you getting anything in the error console? I'm pretty certain by now that you don't want to use $querySetup at this point too. What is the value you want to pass to script.php from index.php and where does it come from?
The $querySetup is a query string (looks like "username1108131343"). It sends the "username1108131343" to script.php where the script uses SQL to select a value in a table where the name of the table is the query string. I don't get any errors, nothing happens when the refresh script is updating.
@Cheezen I assumed from your question that $querySetup was an integer. I've removed the integer cast from my answer, try it now. BTW, allowing the table name to be passed as a query var is a very bad idea
Your code works as well, did you change anything more than the integer cast? Removed it myself with your previous code but wouldn't work.
|
0

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

<? $querySetup ?>

You probably want one of these:

<? echo $querySetup ?>
<?=$querySetup ?>
answered Aug 11, 2011 at 7:02

Comments

0

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

Bill the Lizard
407k213 gold badges577 silver badges892 bronze badges
answered Aug 11, 2011 at 9:40

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.