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....

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>//jQuery Plugin
<?php 
if(!empty($_GET['a1'])){ $selected = $_GET['a1'];}
else{ $selected = 'home';}
?>
<form action="" method="post">
    <label>
        <input type="radio" name="a1" value="home" /> Home 
    </label></br>
    <label>
        <input type="radio" name="a1" value="site1" /> Site 1 
    </label></br>
    <label>
        <input type="radio" name="a1" value="site2" /> Site 2 
    </label></br>
</form>
<span class="r-text"><?php echo $selected;?></span>
<script>
    $('input[type=radio]').click(function(e) {//jQuery works on clicking radio box
        var 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);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id="myform" action="" method="post">
  <input type="radio" name="a1" value="home" /> home</br>
  <input type="radio" name="a1" value="site1" /> site1</br>
  <input type="radio" name="a1" value="site2" /> site2</br>
  <div id="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.

<div id="radioMsg"></div>
<form action="" method="post">
         <input type="radio" name="a1" value="home" onchange="showRadio()"  /> Home <?php echo ($selected == 'home' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site1" onchange="showRadio()" /> Site 1 <?php echo ($selected == 'xyz' ? 'This was selected!' : '');?> </br>
         <input type="radio" name="a1" value="site2" onchange="showRadio()" /> Site 2 <?php echo ($selected == 'zbc' ? 'This was selected!' : '');?> </br>
</form>

Meanwhile in showRadio():

function showRadio(){
    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"