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
https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/
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
https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260972
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
https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260974
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.