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.

4
  • 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. Commented May 20, 2012 at 9:19
  • I did check that , and it shows the correct id Commented May 20, 2012 at 9:27
  • possible duplicate of 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: stackoverflow.com/… Commented May 20, 2012 at 9:29
  • did you check if ID in JS and PHP is really 0? Commented May 20, 2012 at 9:39

3 Answers 3

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); } 
    });   
Sign up to request clarification or add additional context in comments.

5 Comments

Dear sir , the alert box shows this : "Vote Status = No Vote Provided - Property = undefined "
Are you getting the ID correctly execute this line and check if its not zero alert($('#ID').val());
yes I am getting and its non zero , i mean am getting the correct value
Do you provided quotes for VoteID ? "VoteID"
IF you are using Firefox with firebug..just make this code change in the php file and see if its non-zero echo $_POST['VoteID'];exit; and look on your console for result
2

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

data: {"VoteID":ID}

3 Comments

data: {"VoteID":ID , "Property":prop} --- the property is shown as undefined in the alert box
@sqlchild: Please learn what json is. api.jquery.com/jQuery.ajax ; php.net/json
sir, what's the error in my code ? there's nothing but still its not working
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); } 
});        

1 Comment

THis is GET method ..so you need to change the type attribute to "GET"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.