954,600 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Send a javascript variable to a php page through AJAX

Hi,

I'm very new to Javascript and AJAX so I'm using a script that I found, and I need it to send a PHP variable from the global variable (in the address bar where it says &post=94 for example) along with the other data that the script sends to a php script.

I've tried to add what I think should work but it clearly doesn't and I have no clue as how the variable would be used once it fetches the $_GET['postID'] (if that is even allowed?).

Any help would be much appreciated, thanks!

The script looks like this:

$(document).ready(function() {
	$('a.delete').click(function(e) {
		e.preventDefault();
		var parent = $(this).parent();
<strong>		var postID = $_GET['postID'];</strong>

		$.ajax({
			type: 'post',
			url: 'remove_row.php',
			data: 'ajax=1&delete=' + parent.attr('id').replace('record-','') + '&post=' +<strong> postID</strong>,
			beforeSend: function() {
				parent.animate({'backgroundColor':'#fb6c6c'},300);
			},
			success: function() {
				parent.slideUp(300,function() {
					parent.remove();
				});
			}
		});
	});
});
cluelessguy
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

If the javascript is already on a PHP page then you can just do

var postID = <?php echo $_GET['postID']; ?>

And keep the rest of your code the same.

ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

Thanks, it worked perfectly!

cluelessguy
Newbie Poster
2 posts since Jan 2010
Reputation Points: 10
Solved Threads: 0
 

i want to do the same thing but send the javascript variable to jsp page using post method.and save the data in data page without reloading

prerna21
Newbie Poster
1 post since Oct 2010
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You