PHP can't $_GET value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

PHP can't $_GET value

Post 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?
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP can't $_GET value

Post 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/
Last edited by Dodon on Wed Aug 10, 2011 6:38 am, edited 1 time in total.
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post 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."'); ?
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP can't $_GET value

Post 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 ?>');
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post by Theramore »

oh ofc. nice, thank you !
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post 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:
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP can't $_GET value

Post 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.
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post by Theramore »

i tried it won't work even without the php variables
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP can't $_GET value

Post 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."');"; ?>
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post by Theramore »

nope doesn't work
Theramore
Forum Newbie
Posts: 22
Joined: Sun Jun 26, 2011 6:53 am

Re: PHP can't $_GET value

Post 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
Dodon
Forum Commoner
Posts: 64
Joined: Wed Aug 03, 2011 4:11 am
Location: Netherlands

Re: PHP can't $_GET value

Post by Dodon »

Ah ok :) Good that it's working now.
Post Reply