简体   繁体   中英

get radio button value (PhP MySQL and ajax)

Newbie here.. I have this code for my displaying the database records in a table, I want to know how do i get the value of radio button (the radio button contains the id of selected row). I tried doing it in javascript but no luck.

table.php

<table border="1">
    <th></th>
    <th>Particulars</th>
    <th>Amount</th>

        <?php
            include('includes/config.php');
            $qry = $con->prepare('SELECT * FROM fees');
            $qry->execute();
            $row = $qry->rowCount();

            while ($row = $qry->fetch(PDO::FETCH_ASSOC)) {
                $id = $row['fee_id'];
                $fee = $row['fee_name'];
                $amt = $row['amount'];                      
        ?>
        <?php echo "

        <tr title='Click to edit or delete record'>
           <td><input type = 'radio' name = 'radio' class ='radio' value = $row[fee_id]></td>
           <td>$fee</td>
           <td>$amt</td>                            
        </tr>

        "; ?>
        <?php } ?>      
</table>
<input type="button" value="Delete Selected Fee" style="width:250px; height:40px;font-weight:bold;" onclick="delete();"><br><br>

javascript code

<script type="text/javascript">
        function delete(){
        if (document.getElementsByClassName(this.class).checked){
            var val = document.getElementsByClassName(this.class).value;
            alert (val);
            }
        }

</script>

I suggest you to use hidden field instead if you don't want to show. The property of radio buttons is to show only true / false value. It may be on / off or yes / no, according to your uses. Use like

<input type = 'hidden' name = 'whatever' class ='radio' value =<?php echo  $row['fee_id']; ?>>

You can try this, this should get your radio button value using your input name.

<script type="text/javascript">
    function delete(){
        if($('input[name="radio"]').is(':checked')){
          var val = $('input[name="radio"]').val();
          alert (val);
        }
    }
</script>

Use this

function delete(){
    if($('input[name="radio"]').is(':checked')){
    var val = $('input[name="radio"]').val();
    console.log(val);
    }
    }

Following code will give you your radio button value

<script type="text/javascript">
    function delete(){
        if($('input[name="radio"]').is(':checked')){
          var val = $('input[name="radio"]').val();
          alert (val);
        }
    }
</script>

you can use console.log(val);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM