I want to Enter my radio button value to the database in php



  • I have tried t enter my radio button value to the database and in the console showing an error.
    I looked everywhere and tried everything to get the selected value from a group of radio buttons and i failed.
    Can anyone help me out.
    here is my code

    admin-map.php

    <?php
    include_once 'header.php';
    include_once 'locations_model.php';
    ?>
    
    
    <div id="map"></div>
    
    <!------ Include the above in your HEAD tag ---------->
    <script>
        var map;
        var marker;
        var infowindow;
        var red_icon =  'http://maps.google.com/mapfiles/ms/icons/red-dot.png' ;
        var purple_icon =  'http://maps.google.com/mapfiles/ms/icons/purple-dot.png' ;
        var locations = <?php get_all_locations() ?>;
    
        function initMap() {
            var france = {lat: 6.8602, lng: 80.0535};
            infowindow = new google.maps.InfoWindow();
            map = new google.maps.Map(document.getElementById('map'), {
                center: france,
                zoom: 7
            });
    
    
            var i ; var confirmed = 0;
            for (i = 0; i < locations.length; i++) {
    
                marker = new google.maps.Marker({
                    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                    map: map,
                    icon :   locations[i][5] === '1' ?  red_icon  : purple_icon,
                    html: document.getElementById('form')
                });
    
                google.maps.event.addListener(marker, 'click', (function(marker, i) {
                    return function() {
                        confirmed =  locations[i][5] === '1' ?  'checked'  :  0;
                        $("#confirmed").prop(confirmed,locations[i][5]);
                        $("#id").val(locations[i][0]);
                        $("#description").val(locations[i][3]);
                        $("#image").attr("src", locations[i][4]);;
                        $("#form").show();
                        infowindow.setContent(marker.html);
                        infowindow.open(map, marker);
                    }
                })(marker, i));
            }
        }
    
        function saveData() {
            var confirmed = document.getElementById('confirmed').checked ? 1 : 0;
            var id = document.getElementById('id').value;
            var Condition=document.getElementById('Condition').value;
    
            var fd = new FormData();
    
            fd.append('confirmed', confirmed);
            fd.append('id', id);
            // fd.append('Condition',Condition);
    
            $.ajax({
                url : 'confirm_location.php',
                type : 'POST',
                data : fd,
                processData: false,
                contentType: false,
                async: false,
                success : function(data) {              
                    alert(data);
    
                    if(data == "Location added.")
                    {
                        infowindow.close();
                        window.location.reload(true);
                        return;
                    }
    
                    infowindow.setContent("<div style='color: purple; font-size: 25px;'>Inserting Errors</div>");
                    
                },
                error : function(request,error)
                {
                    alert(error);
                }
            });
        }
    
    
        function downloadUrl(url, callback) {
            var request = window.ActiveXObject ?
                new ActiveXObject('Microsoft.XMLHTTP') :
                new XMLHttpRequest;
    
            request.onreadystatechange = function() {
                if (request.readyState == 1) {
                    callback(request.responseText, request.status);
                }
            };
    
            request.open('GET', url, true);
            request.send(null);
        }
    
    
    </script>
    
    <div style="display: none" id="form">
        <table class="map1">
            <tr>
                <image id="image" src="" style="width: 250px; height: auto;">
                <input name="id" type='hidden' id='id'/>
                <td><a>Description:</a></td>
                <td><textarea disabled id='description' placeholder='Description'></textarea></td>
            </tr>
            <tr>
                <td><b>Confirm Location ?:</b></td>
                <td><input id='confirmed' type='checkbox' name='confirmed'></td>
            </tr>
            <tr>
                <td><b>Set Condition</b></td>
                <td><input type="radio" name="Condition" value="1" >Normal</td>
                <td><input type="radio" name="Condition" value="2" >Bad </td>
                <td><input type="radio" name="Condition" value="3">Very Bad</td><input>
            </tr>
    
            <tr><td></td><td><input type='button' value='Save' name="submit_btn" onclick='saveData()'/></td></tr>
        </table>
    </div>
    <script async defer
            src="https://maps.googleapis.com/maps/api/js?language=en&key=AIzaSyA-AB-9XZd-iQby-bNLYPFyb0pR2Qw3orw&callback=initMap">
    </script>
    
    
    

    confirm_location.php

    <?php
    
    if(isset($_POST['confirmed']) && isset($_POST['id']))
    {
    	$con=mysqli_connect ("localhost", 'root', '','demo');
    
    	if (!$con) {
    	    die('Not connected : ' . mysqli_connect_error());
    	}
    
    	$id = mysqli_real_escape_string($con, $_POST['id']);
    	$confirmed = mysqli_real_escape_string($con, $_POST['confirmed']);
    	$Condition = mysqli_real_escape_string($con,$_POST['Condition']);
    
    	// update location with confirm if admin confirm.
    	$query = "update locations set location_status = $confirmed WHERE id = $id ";
    	$query_condition="UPDATE locations set locationCondition = $Condition where id=$id";
    
    	$result = mysqli_query($con,$query);
    	// $result2=mysqli_query($con,$query_condition);
    
    	if ($result) {
    	    echo 'Location added.';
    	    return;
    	}
    
    	echo 'Can\'t add location';
    }
    else
    {
    	echo 'Please enter required fields';
    }
    
    


  • in your saveData() method

    function saveData() {
            var confirmed = document.getElementById('confirmed').checked ? 1 : 0;
            var id = document.getElementById('id').value;
            var Condition=$('input[name= Condition]:checked').val(); //get the radio button value like this, it's easy to use jquery than javascript
    
            var fd = new FormData();
    
            fd.append('confirmed', confirmed);
            fd.append('id', id);
            fd.append('Condition',Condition); //uncomment this line
    }
    


  • @dev_lak Thankx A lot



  • @akashmanujaya ur welcome 😃



  • @dev_lak Your code is not working. i already set the column for default value as 1. but i check third radio button which value is 3, it still input 1 as the value. Why is that? please help me out



  • @dev_lak I found the error. its working perfecly.. thanks for your help



  • @akashmanujaya i'll check



  • @akashmanujaya i'll check



  • @akashmanujaya i have done small change to code, try this

    function saveData() {
            var confirmed = document.getElementById('confirmed').checked ? 1 : 0;
            var id = document.getElementById('id').value;
            var Condition=$('input[name="Condition"]:checked').val(); //check this line
     
            var fd = new FormData();
     
            fd.append('confirmed', confirmed);
            fd.append('id', id);
            fd.append('Condition',Condition); //uncomment this line
    }