Jump to content

PHP $_get wont work


JohnOP

Recommended Posts

Test code

 

test.php

<script type="text/javascript">
$(document).ready(function(){

$("#sub").click(function(){
$("#test").load("sub.php");
$.get("sub.php", { 
uid: $("#sub").attr('uid') 
});
});
});

</script>
<a href="#" id="sub" uid="1">Sub</a>
<div id="test">

</div>

 

sub.php

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

 

I am trying to load a file after clicking the href which has an extra attribute so i can query the database with it, however the get method seems to be not working, it works on other pages though.

 

Error message i get

 

Notice: Undefined index: uid in C:\xampp\htdocs\sub.php on line 2

Link to comment
Share on other sites

So you want to resend headers without refreshing a page?

 

I'm not sure this is possible - you're approaching it the wrong way. You want the PHP parsing to be done via AJAX, and the main page updated with the return values of that script.

Link to comment
Share on other sites

Use AJAX. I'm not familiar with jQuery, so I can't help you with that side of things.

 

If $_GET isn't working, it means the browser isn't sending that information in the query string header. It's a client-side/jQuery issue.

Link to comment
Share on other sites

This isn't really a PHP question. However, please see the following. I believe this is what you are trying to accomplish.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>PhpFreaks Test</title>
	<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
	<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
	<script type="text/javascript">
	$(document).ready(function() {
		$('a.sub').bind("click", function() {
			$.ajax({
				url: 'temp.php',
				data: 'uid=' + $(this).attr('id'),
				success: function(response) {
					$('#test').html(response);
				}
			});
			return false;
		});
	});
	</script>
</head>
<body>
	<h3>Test Script</h3>
	<p>
		<a href="#" id="1" class="sub">Sub 1</a>
		<a href="#" id="2" class="sub">Sub 2</a>
		<a href="#" id="3" class="sub">Sub 3</a>
		<a href="#" id="4" class="sub">Sub 4</a>
		<a href="#" id="5" class="sub">Sub 5</a>
	</p>
	<h3>Test Section</h3>
	<p id="test"> </p>
</body> 
</html> 
<?php
#Only because it's self, remove this statement for sub.php
if (count($_GET) > 0) {
ob_end_clean();
if (isset($_GET['uid'])) {
	echo $_GET['uid'];
}
else {
	echo 'Never use undefined variables. That\'s what that "Notice Error" is.';
}
}
?>

Link to comment
Share on other sites

I don't know why you are using both .load and .get (they do similar things.)

 

Your use of .load is what is causing the undefined error, because it is requesting the sub.php page but is not supplying any data and if it was supplying data, it would be sent as $_POST data.

 

Your use of .get is not using the returned data anyway. You would need to supply a callback function as part of the .get syntax.

 

Why not just use .load -

<script type="text/javascript">
$(document).ready(function(){

$("#sub").click(function(){
	$("#test").load("sub.php",{uid: $("#sub").attr('uid')});
});
});

</script>
<a href="#" id="sub" uid="1">Sub</a>
<div id="test">

</div>

 

<?php
$id = $_POST['uid'];
echo $id;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.