0

I need to receive the status of vote using ajax and php and jquery. Following is my code :

var VoteStatus= GetStatus() ;
var ID = $('#ID').val();

function GetStatus() {
    var res = '';

    $.ajax({
        type: "POST",
        data: {VoteID:ID} ,
        url: "/vote_status.php",
        async: false,       
        success: function(result) { res=result; } 
    });                  

    return res;
}

alert('Vote Status= ' + VoteStatus);

In my php file:

$VoteID = $_POST['VoteID'];
$Property = $_POST['Property'];



if ( $VoteID == 0 ) 
    echo 'No Vote provided - Property = '. $Property;

exit;

The alert box shows: Vote Status = No Vote Provided

Please help.

I have posted the VoteID, but the php file doesn't seem to receive it.

sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • If the alert box says "No vote provided, then your php script should be working well. Check what this line var ID = $('#ID').val(); returns. – Chibuzo May 20 '12 at 09:19
  • I did check that , and it shows the correct id – sqlchild May 20 '12 at 09:27
  • possible duplicate of [How can I use AJAX to fetch data and store it in javascript variables?](http://stackoverflow.com/questions/8570524/how-can-i-use-ajax-to-fetch-data-and-store-it-in-javascript-variables) - and the many, many, many, many, many, many, many, many others: http://stackoverflow.com/search?q=jQuery+ajax+fetch+data+from+PHP+into+a+jQuery+variable – hakre May 20 '12 at 09:29
  • did you check if ID in JS and PHP is really 0? – mistapink May 20 '12 at 09:39

3 Answers3

2

The name of the POST variable needs to be in quotes, as in

data: {"VoteID":ID}
msgmash.com
  • 1,035
  • 5
  • 10
2

Try the alert in here and check if its working

 $.ajax({
        type: "POST",
        data: {"VoteID":ID} ,
        url: "/vote_status.php",
        async: false,       
        success: function(result) { 
  alert(result); } 
    });   
coolguy
  • 7,866
  • 9
  • 45
  • 71
2

Try this and check jquery ajax manuals

$.ajax({
    type: "POST",        
    data:"VoteID=" + ID +"&secondparam=" + secondvalue,
    url: "/vote_status.php",
    async: false,       
    success: function(result) { alert(result); } 
});        
Justin John
  • 9,223
  • 14
  • 70
  • 129