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
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?

Link to comment
Share on other sites

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