Jump to content

Recommended Posts

So i'm sending id value trough post in json format.

When i open Firebug it returns the correct value but in php it's keep returning null.

 

$('.stars').bind('click', function() {  
	var star = this;
	  var data = {'q' : $(star).attr('id') };
	$.ajax({
                type:'post',
			url:"voting.php",
			data:data,
			 dataType:'json'
	     }); 
		 return false;
        });

 

 

<?php
$q=$_POST['q'];
echo json_encode($q);
?>

Link to comment
https://forums.phpfreaks.com/topic/258850-php-post-and-jquery-problem/
Share on other sites

You haven't told PHP to look for any results.

 

$('.stars').bind('click', function() {  
	var star = this;
	  var data = {'q' : $(star).attr('id') };
	$.ajax({
                type:'post',
			url:"voting.php",
			data:data,
			 dataType:'json',
			success: function(result){ alert(result); }
	     }); 
		 return false;
        });

You might be missing out on the A in AJAX.

...Uh, the first one.

 

You can't do anything with the result unless you put it in that success function. If you try anywhere else you're going to get a null/undefined because the AJAX hasn't happened yet. Pretend that it takes 5 minutes for anything to happen: you can't get the answer immediately so you have to put in some function that'll execute later (ie, in five minutes) when it does have the answer. Meanwhile you just wait for it to come back.

  $.ajax({
                type:'post',
			url:"voting.php",
			data:data,
			 dataType:'json',
			success: function(response) { alert(response); }


	     }); 

 

Still appear null in 'voting.php' page but the alert is showing the right value.

 

I don't think you're using the right function for what you want. When Ajax submits a request like that voting.php is only opened on the server, with the response sent back to your machine. You'll never be able to see what's in voting.php on your machine.

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.