SharkBait Posted August 28, 2010 Share Posted August 28, 2010 Ok this is bugging me, I'm trying to do a simple ajax request via a click of a link and even though Success triggers, the response is empty. <?php $pid = '123456789'; ?> <script> $(document).ready(function() { $('#clickme').click(function() { alert('You have clicked me'); $.ajax({ url: 'getphoto.php', type: 'GET', data: {pid: '<?php echo $pid;?>'}, dataType: 'html', success: function(data) { alert('Data Sent: '+ data); } }); }); }); </script> Then the php file: <?php if(isset($_GET['pid])) { $pid = $_GET['pid']; } echo 'You wanted to show a PID of: '. $pid; ?> Why do I get does this successed, but not return anything for `data` Link to comment https://forums.phpfreaks.com/topic/211954-jquery-ajax-and-response-empty/ Share on other sites More sharing options...
SharkBait Posted August 28, 2010 Author Share Posted August 28, 2010 Well it seems if within the .ajax() if I set async: false that worked, but then I had a weird reloading issue but putting event.preventDefault(); before the $.ajax seemed to fix that issue. Is this just some weird behaviour I am seeing or typical operating procedure? Link to comment https://forums.phpfreaks.com/topic/211954-jquery-ajax-and-response-empty/#findComment-1104619 Share on other sites More sharing options...
trq Posted August 28, 2010 Share Posted August 28, 2010 What is #clickme ? Link to comment https://forums.phpfreaks.com/topic/211954-jquery-ajax-and-response-empty/#findComment-1104707 Share on other sites More sharing options...
SharkBait Posted August 29, 2010 Author Share Posted August 29, 2010 Oh yea.. <a id="clickme" href="#">Click Me</a> Sort of thing Link to comment https://forums.phpfreaks.com/topic/211954-jquery-ajax-and-response-empty/#findComment-1104899 Share on other sites More sharing options...
Alex Posted August 29, 2010 Share Posted August 29, 2010 You have an error in your PHP file. You're missing a ' on the second line. Try this: <?php if(isset($_GET['pid'])) { $pid = $_GET['pid']; } echo 'You wanted to show a PID of: '. $pid; ?> By the way, you're probably going to want the change the logic in that code, it's pretty flawed. The purpose of isset is to be able to check if a variable is set without having an undefined variable notice thrown, but in your case if $_GET['pid'] isn't set than $pid won't be set and you'll get an undefined variable notice for that. Link to comment https://forums.phpfreaks.com/topic/211954-jquery-ajax-and-response-empty/#findComment-1104927 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.