Amol
Amol

Reputation: 69

How to check radio button is checked or not in cakePHP

I have two radio buttons, I want to generate the event after clicking on radio button and check which radio button is checked in PHP controller.

My source code for radio buttons,

<?php
    echo '<label class="radio inline">';
    printf('<input class="radio" type="radio" name="Comments" id=radioShowAllComments" value = "all comments" checked="checked"/>');
    echo '<span class="'. ''.'">'.__('Show All Comments').'</span>';
    echo '</label>';

    echo '<label class="radio inline">';
    printf('<input class="radio" type="radio" name="Comments" id=radioOnlyMembersComments" value = "specific comments" />');
    echo '<span class="'. ''.'">'.__('Display Only Members of Comments').'</span>';
    echo '</label>';
?>

Upvotes: 1

Views: 1098

Answers (4)

Ataboy Josef
Ataboy Josef

Reputation: 2101

I hope you are using the latest version(CakePHP3x) for your project.

Now in your controller, use: $this->Request->data('Comments') [for GET method forms, use $this->Request->query('Comments') instead] to take the selected option out in the controller.

You will get more details to work on this from the official documentation here.

Upvotes: 0

Asim Khan
Asim Khan

Reputation: 55

On your controller you can use this

$optComments=$this->request['Comments'];
$somevar=($optComments=='specific comments')?yourFunction():otherFunction();

Or you may peroform on client side with jquery ajax

 <script>
     $(document).on("click", ".radio[name=Comments]", function(){
        var value = $(this).val();
    var url = "default url"; 
      if(value=='specific comments'){
    url = "some url";
    }else{
    url = "some url";
    }
    $.ajax({url:url;method'POST';data:{}success:function(data){}})
    });
</script>

This is sample code you might perform some outstanding functions on basis of this.

Upvotes: 0

HEMANT SUMAN
HEMANT SUMAN

Reputation: 488

Use the form id #myForm -

  $('#myForm').on('change', function() {
       alert($('input[name=Comments]:checked').val()); 
    });

Upvotes: 0

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : You can register a click event handler for radio button and then read value of checked radio button.

$(document).on("click", ".radio[name=Comments]", function(){
    var value = $(this).val();
    alert(value);
});

Upvotes: 1

Related Questions