1

I am fetching data from MySQL using Javascript. I looked and tried everything (from id, from class, from name) to get the selected value from radio button, but without success.

I tried all these options but nothing works..

$('.action').val(data.action); 

$('#action').val(data.action); 

$("input[name='action']:checked").val(data.action); 

Here is the piece of my HTML code..

 <form method="post" id="insert_form">
  <div class="form-group">

    <div class="form-group">
      <div class="form-check form-check-inline">
        <label for="action">ACTION: &nbsp; &nbsp;</label>
          <label class="form-check-label">
            <input type="radio" name="action" value="buy" class="form-check-input action"> Buy
          </label>

          <label class="form-check-label">
            <input type="radio" name="action" value="sell" class="form-check-input action" > Sell
          </label>
        </div>
          <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-primary" /> 
      </div>
  </form>
  </div>

... and here is the piece of my Javascript code..

  $(document).on('click', '.edit_data', function(){  
       var signal_id = $(this).attr("id");  
       $.ajax({  
            url:"fetch.php",  
            method:"POST",  
            data:{signal_id:signal_id},  
            dataType:"json",  
            success:function(data){
                $('#pair').val(data.pair);  
                $('#entry').val(data.entry);
                //$('.action').val(data.action); 
                $("input[name='action']:checked").val(data.action); 
                $('#direction').val(data.direction); 
                $('#t_p').val(data.t_p);
                $('#s_l').val(data.s_l);
                $('#add_analysis').val(data.add_analysis);  
                $('#notes').val(data.notes);   
                $('#signal_id').val(data.id);  
                $('#insert').val("Update"); 
                $('#add_data_Modal').modal('show');  

            }  
       });  
  });
Daedalus
  • 7,586
  • 5
  • 36
  • 61
mactron
  • 61
  • 1
  • 1
  • 7
  • Possible duplicate of [How can I know which radio button is selected via jQuery?](https://stackoverflow.com/questions/596351/how-can-i-know-which-radio-button-is-selected-via-jquery) – Sean Dec 11 '17 at 04:40
  • What is `.edit_data` here? I can not see this class anywhere in your html code. – Himanshu Upadhyay Dec 11 '17 at 04:41
  • @Himanshu Upadhyay button .. I think that the problem is in radio buttons, because other values are fetched ! – mactron Dec 11 '17 at 04:46
  • @Pete But that code is not present in your question. So you either gave us pseudo code(not the real thing), or you are targeting html that doesn't exist. – Daedalus Dec 11 '17 at 04:47
  • Are you trying to set values on radio btns or you are trying to get the value on your radio btns? – Eddie Dec 11 '17 at 04:49
  • @EddieI am trying to fetch value of radio button from MySQL.. – mactron Dec 11 '17 at 04:51
  • Please see my answer below on how to set the value of radio btn. Btw, What is the expected value for `data.action`? – Eddie Dec 11 '17 at 04:53

1 Answers1

1

Based on what I understand, you are trying to set the values on the radio buttons based on the result/response from ajax/MySQL.

This is how $('input:radio').prop('checked',true); to set if radio button is selected or not.

$(document).ready(function(){
 //temporary code
 var action = 1;
    
 if ( action == 1 ) $('input:radio[value=buy]').prop('checked',true);
 else $('input:radio[value=sell]').prop('checked',true);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<input type="radio" name="action" value="buy" class="form-check-input action"> Buy
<input type="radio" name="action" value="sell" class="form-check-input action"> Sell
Eddie
  • 26,593
  • 6
  • 36
  • 58