Skip to content
Advertisement

how to get the data from ajax request then use it in php conditional statemtn

I want to create an ajax post request that gets the value of the radio button then use it in a PHP conditional statement.

So far i have tried this code (all code is from one php file):

$(document).ready(function () {
    $('.radio-buttons input[type="radio"]').click(function(){
        var subject= $(this).val();

        $.ajax({
            url: 'home.php',
            type: 'POST',
            data: {
                'subject': subject
            },
            success: function(response) {
                alert(response); 
            }               
        });
    });
});
<form id="form1" method="POST" class="radio-buttons ">
     <input class="radio-filter"  type="radio" name="subject" value="A">A</input> 
     <input class="radio-filter"  type="radio" name="subject" value="B">B</input>            
</form>
if (isset($_POST['subject'])) {
    echo "Showing!";
}

the alert message shows the value of the radio button when I clicked them but the echo in PHP condition is not showing.

Advertisement

Answer

You are using alert(subject); which will just alert the value of the radio button as you’ve mentioned.

Change that line to alert(response); to alert the response on success.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement