Jump to content

jQuery Ajax and Response Empty?


SharkBait

Recommended Posts

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

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?

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.

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.