Question

I want to make a voting system using PHP, MySQL and jQuery.

The idea is that when you click on a radiobutton, the vote is inserted into the database.

My question is:

How I can run the query to the database with PHP, detecting the change in the radiobutton using jQuery?

This is my basic idea:

if ($("input:radio[name=vote]:checked").val()){

//HERE I WANT TO RUN A QUERY TO MYSQL WITH PHP

}

I do this because this way if you choose a radiobutton, then inserted a vote for that option. But if I choose another radiobutton (vote for another option), then update the vote.

Thank you very much.

Greetings.

Was it helpful?

Solution

You can make an ajax call when you detect a click on a radio button. It'll hit a PHP file and you can then insert the vote from there. Check out jQuery Ajax for examples.

Example:

$.ajax({
    type : 'POST',
    url : '/ajax/vote.php',
    data : {
        value : radio_button_val
    },
    beforeSend : function() {
        // loading image
    },
    success : function(data) {
        // successful
    },
    error : function() {
        // failed
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top