Page 1 of 1

PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:19 am
by Theramore
Hey there guys!

I got a little problem.
I tried to create a real time page with connection to mysql db and grab data in real time but, the query depends of $_GET, function that my file can't preform, what other ways are to get values ?

Here is the example code:

Index.php:

<head>
<script src='js/jQuery.js' type='text/javascript' />
</head>

<div class='viewdata'>

</div>
<script src='js/js_view_data.js' type='text/javascript' />

Js_view_data.js:

$.ajaxSetup({
cache: false;
});

$(setInterval(function() {
$(".viewdata").load("viewdata.php");
$(".viewdata").attr({ scrollTop: $(".viewdata").attr("scrollHeight") });

}, 500));


Viewdata.php:

Code: Select all

mysql_connect("blabla", "blabla", "blabla");
mysql_select_db("somedb");

$id = $_GET['id'];
$query = mysql_query("SELECT whateverdamnthing FROM whatevertable WHERE id = '$id' ");

while($row = mysql_fetch_array($query)){

echo $row['whatever'];

}

Now, the problem is that index.php is the one who can $_GET , so any $_GET in placed in this file will be 0.

Can anyone help me?

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:34 am
by Dodon
You have to adjust your link to viewdata.php to that it contains the GET variables

$(".viewdata").load("viewdata.php");

becomes

$(".viewdata").load("viewdata.php?id=12345");

where 12345 is the id you want to load.

You can do it in a different way as well http://api.jquery.com/load/

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:36 am
by Theramore
but the id in link is not always its the same

shall i place the js script in a php file and use .load('viewdata.php?id=".$id."'); ?

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:40 am
by Dodon
yes but use

.load('viewdata.php?id=<?=$id?>');

and if you don't have short open tags on then use

.load('viewdata.php?id=<?php echo $id ?>');

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:44 am
by Theramore
oh ofc. nice, thank you !

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:53 am
by Theramore
won't work , take a look:

Test.php:

Code: Select all

<?php
$id = $_GET['id'];
?>

<head>
<script src='js/jQuery.js' type='text/javascript' />
</head>

<div class='test'>

</div>

<script type='text/javascript'>

$.ajaxSetup({
cache: false;
});

$(setInterval(function() {

<?php echo "$('.test').load('testdata.php?id=".$id."');"; ?>
$(".test").attr({ scrollTop: $(".test").attr("scrollHeight") })

}, 500));

</script>
Testdata.php

Code: Select all

<?php

$id2 = $_GET['id'];

echo $id2;

?>

so if i do this : http://localhost/test.php?id=2

it won't echo 2 8O :banghead:

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 6:59 am
by Dodon
What is the value of $_GET['id'] at the start of test.php? if it's empty it won't echo anything in testdata.php.

and if you use .load('testdata.php?id=2'); does it work then? So just for testing purposes don't use the PHP variable.

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 7:07 am
by Theramore
i tried it won't work even without the php variables

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 7:09 am
by Dodon
Ooh just saw the last part of you previous reply.

try to change the following:

Code: Select all

<div class='test'> 
</div>
into

Code: Select all

<div id='test'> 
</div>
and

Code: Select all

<?php echo "$('.test').load('testdata.php?id=".$id."');"; ?>
into

Code: Select all

<?php echo "$('#test').load('testdata.php?id=".$id."');"; ?>

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 7:30 am
by Theramore
nope doesn't work

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 7:36 am
by Theramore
sorry man , got it working:) lol
the problem was in java code:
$.ajaxSetup ({
cache: false;
});

My mistake the : false;
i shouldn't use ; there lol

Re: PHP can't $_GET value

Posted: Wed Aug 10, 2011 7:48 am
by Dodon
Ah ok :) Good that it's working now.