Skip to content Skip to sidebar Skip to footer

Echo Radio Button Value Without Using Submit Button In Php

I want to echo the selected radio button value without using submit button. I want to display the value of radio button when it is selected. Here is the code.

Solution 1:

Here is your solution....

<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>//jQuery Plugin
<?phpif(!empty($_GET['a1'])){ $selected = $_GET['a1'];}
else{ $selected = 'home';}
?><formaction=""method="post"><label><inputtype="radio"name="a1"value="home" /> Home 
    </label></br><label><inputtype="radio"name="a1"value="site1" /> Site 1 
    </label></br><label><inputtype="radio"name="a1"value="site2" /> Site 2 
    </label></br></form><spanclass="r-text"><?phpecho$selected;?></span><script>
    $('input[type=radio]').click(function(e) {//jQuery works on clicking radio boxvar value = $(this).val(); //Get the clicked checkbox value
        $('.r-text').html(value);
    });
</script>

Solution 2:

I'm not sure how you want to do a clientside manipulation with PHP(!!!) but this is a jquery solution for displaying the value of radio button when it is selected:

$('#myform input[type=radio]').on('change', function(event) {
  var result = $(this).val();
  $('#result').html(result);
})
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><formid="myform"action=""method="post"><inputtype="radio"name="a1"value="home" /> home</br><inputtype="radio"name="a1"value="site1" /> site1</br><inputtype="radio"name="a1"value="site2" /> site2</br><divid="result"></div></form>

Solution 3:

You need to use the onchange event of the radio buttons with javascript/jquery.

PHP runs server-side, not client-side, so without sending client-side changes to the server, it can't output stuff based on them: you'd need to send a $_POST or a $_GET request, either by submitting the form or using AJAX. Not necessary for this.

<divid="radioMsg"></div><formaction=""method="post"><inputtype="radio"name="a1"value="home"onchange="showRadio()"  /> Home <?phpecho ($selected == 'home' ? 'This was selected!' : '');?></br><inputtype="radio"name="a1"value="site1"onchange="showRadio()" /> Site 1 <?phpecho ($selected == 'xyz' ? 'This was selected!' : '');?></br><inputtype="radio"name="a1"value="site2"onchange="showRadio()" /> Site 2 <?phpecho ($selected == 'zbc' ? 'This was selected!' : '');?></br></form>

Meanwhile in showRadio():

functionshowRadio(){
    var radioVal = $("input[name='a1']:checked").val();
    if(radioVal) {
        $( "#radioMsg" ).html("<p>"+radioVal+"</p>");
    }
}

I'm not sure where you want the changed button's value outputted, so I'm putting it into that div just as an example.

To define the onchange event in an external stylesheet instead of inline:

$( "input[name='a1']" ).change(function(){
    ... (showRadio's contents go here)
});

Post a Comment for "Echo Radio Button Value Without Using Submit Button In Php"