sqlchild
sqlchild

Reputation: 9074

jQuery ajax fetch data from PHP into a jQuery variable

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.

Upvotes: 0

Views: 795

Answers (3)

Justin John
Justin John

Reputation: 9616

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); } 
});        

Upvotes: 2

coolguy
coolguy

Reputation: 7954

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); } 
    });   

Upvotes: 2

msgmash.com
msgmash.com

Reputation: 1035

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

data: {"VoteID":ID}

Upvotes: 2

Related Questions