0

I want to fetch data from mysql database back into radio buttons using jQuery. I want to have the respective radio button checked when I retrieve the data from the database so that I can update. I have written the following code but its not working.

Extract #1 from: reg_number.php

    $(document).on('click', '.update', function(){
        var reg_number_id = $(this).attr("id");
        var btn_action = 'fetch_single';
        $.ajax({
            url:'reg_number_action.php',
            method:"POST",
            data:{reg_number_id:reg_number_id, btn_action:btn_action},
            dataType:"json",
            success:function(data)
            {
                $('#reg_numberModal').modal('show');
                if (data.current_condition == 'Bad')
                {
                    $('#Bad').attr('checked','true');
                }
                else if (data.current_condition == 'Moderate')
                {
                    $('#Moderate').attr('checked','true');
                }
                else if (data.current_condition == 'Good')
                {
                    $('#Good').attr('checked','true');
                }
                $('.modal-title').html("<i class='fa fa-pencil-square-o'></i> Edit Vehicle");
                $('#reg_number_id').val(reg_number_id);
                $('#action').val('Edit');
                $('#btn_action').val('Edit');
            }
        })
    });

Extract #2 from: reg_number.php

                            <div class="row">
                                <div class="col-md-2" align="center">
                                    <input type="radio" id="Bad" name="current_condition" value="Bad">
                                </div>
                                <div class="col-md-2" align="center">
                                    <input type="radio" id="Moderate" name="current_condition" value="Moderate">
                                </div>
                                <div class="col-md-2" align="center">
                                    <input type="radio" id="Good" name="current_condition" value="Good">
                                </div>
                            </div>

Extract #3 from: reg_number_action.php

if
    (
        isset($_POST['btn_action'])
    )
{
    if
        (
            $_POST['btn_action'] == 'fetch_single'
        )
    {
        $query = 
            "
                SELECT 
                    * 
                FROM 
                    tbl_reg_number 
                WHERE 
                    reg_number_id = :reg_number_id
            "
        ;
        $statement = $connect->prepare($query);
        $statement->execute
            (
                array
                    (
                        ':reg_number_id'    =>  $_POST["reg_number_id"]
                    )
            )
        ;
        $result = $statement->fetchAll();
        foreach($result as $row)
        {
            $output['current_condition']= $row['current_condition'];
        }
        echo json_encode($output);
    }

    if
        (
            $_POST['btn_action'] == 'Edit'
        )
    {
        $query = 
            "
                UPDATE 
                    tbl_reg_number 
                SET
                    current_condition = :current_condition

                WHERE 
                    reg_number_id = :reg_number_id
            "
        ;
        $statement = $connect->prepare($query);
        $statement->execute
            (
                array
                    (
                        ':current_condition'=>  $_POST["current_condition"],
                        ':reg_number_id'    =>  $_POST["reg_number_id"]
                    )
            )
        ;
        $result = $statement->fetchAll();
        if
            (
                isset($result)
            )
        {
            echo 'Vehicle edited';
        }
    }
}

?>
lgT2
  • 1
  • 1
  • Hi What does not working mean? What is the error you are seeing ? – tousif Nov 09 '20 at 18:31
  • First, figure out if you have an AJAX or a PHP problem by getting your PHP code to work _without_ AJAX, just by itself. I'm also seeing a lot of `if` without `else`, avoid that as much as possible because it will eventually come to bite you. For MySQL functions, many return an error code if they fail, always check that to be certain, and/or [turn on better error reporting](https://stackoverflow.com/a/22662582/231316). – Chris Haas Nov 09 '20 at 18:35
  • No error, actually, to make the code smaller for the sake of asking directly where I need help, I have removed some fields (input fields) which are supposed to be retrieved from the database together with radio buttons. These fields are retrieving data perfectly except for the radio buttons. – lgT2 Nov 10 '20 at 07:04

0 Answers0