JohnOP Posted August 23, 2011 Share Posted August 23, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/ Share on other sites More sharing options...
xyph Posted August 23, 2011 Share Posted August 23, 2011 <a href="sub.php?uid=value" id="sub" uid="1">Sub</a> Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260956 Share on other sites More sharing options...
JohnOP Posted August 23, 2011 Author Share Posted August 23, 2011 <a href="sub.php?uid=value" id="sub" uid="1">Sub</a> That's the obvious way but i don't want the page refreshed. Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260959 Share on other sites More sharing options...
xyph Posted August 23, 2011 Share Posted August 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260963 Share on other sites More sharing options...
JohnOP Posted August 23, 2011 Author Share Posted August 23, 2011 What i want is after the link is clicked it will call the sub.php page which enters the users id into the database without the page refreshing, to do that i need to pass the id to get to sub.php with refreshing the page. Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260964 Share on other sites More sharing options...
xyph Posted August 23, 2011 Share Posted August 23, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260968 Share on other sites More sharing options...
spfoonnewb Posted August 23, 2011 Share Posted August 23, 2011 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.'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260972 Share on other sites More sharing options...
requinix Posted August 23, 2011 Share Posted August 23, 2011 $("#test").load("sub.php"); That's loading sub.php without anything in $_GET. (You then do a second AJAX thing with .get, which works fine, but discard the results.) Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260973 Share on other sites More sharing options...
PFMaBiSmAd Posted August 23, 2011 Share Posted August 23, 2011 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/245504-php-_get-wont-work/#findComment-1260974 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.