-1

I'm not a dev, I never do it but for a specific thing I've made a web page where people can insert data into a sql database.

My code is working but I guess I can optimize it because I've made it from things I've found over the Internet and I would like to turn radio buttons into check boxes because it is a lot of buttons at the end when there are many lines. Maybe there are things wrong because I have change names so there are no ref to my company.

<!DOCTYPE html>

<?php
include('Connexion_DB.php');

/* Queries */

if (isset($_POST['Btn_Mode1'])){

        $req1 = "UPDATE DATABASENAME SET Mode1 = '".$_POST['Mode1']."' WHERE ID = '".$_POST['ID']."'";
        $result1 = sqlsrv_query($con,$req1);
}
if (isset($_POST['Btn_Mode2'])){

        $req2 = "UPDATE DATABASENAME SET Mode2 = '".$_POST['Mode2']."' WHERE ID = '".$_POST['ID']."'";
        $result2 = sqlsrv_query($con,$req2);
}

if (isset($_POST['form1']))
{ 
    $Status = 'Y';
    $array = explode(PHP_EOL, $_POST['textareaname']);

    foreach($array as $index=>$value) {
    if($value === '') unset($array[$index]);
    }

    foreach ($array as $key => $value) {
    $req = "INSERT INTO DATABASENAME (DATA, Active, Mode1, Mode2) VALUES ('".$value."','".$Status."', 'false','false');";
    $result = sqlsrv_query($con,$req);
    }
    echo "<pre>";   
    echo print_r($array);   
    echo "</pre>";
}

if (isset($_POST['form2'])) {

        foreach ($_POST['ID'] as $id) {
            $req3 = "DELETE FROM DATABASENAME WHERE ID = '".$id."'";
            $result3 = sqlsrv_query($con,$req3);
        }   
    }

include('menu.php');
?>

<html>
    <head>
    <title>TITLE</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" >
</head>

<script>
    $(function() {
        $('.RadioBtn_Mode1').change(function() {

            Mode1 = $(this).val();
            ID = $(this).attr('id');
            $.post( "Test_Page.php", { 
                Btn_Mode1: true, 
                ID: $(this).attr('id'), 
                Mode1: $(this).val()
            } ).done(function( ) {
                toastr.success(ID +' est a ' +Mode1);
            });

        });
    });

    $(function() {
        $('.RadioBtn_Mode2').change(function() {

            Mode2 = $(this).val();
            ID = $(this).attr('id');
            $.post( "Test_Page.php", { 
                Btn_Mode2: true, 
                ID: $(this).attr('id'), 
                Mode2: $(this).val() 
            } ).done(function( ) {
                toastr.success(ID + ' est a ' +Mode2);
            });

        });
    });

</script>


<body>


<div class="container body-container">
    <div id='titre'class='titre'><h1>Insert Data</h1></div>
        <form method='post' >
            <input type="hidden" name="form1" />
                <br />
                <p>Data :</p>
                <textarea name="textareaname" cols="60" rows="5"></textarea>
                <br />
                <br />
                <input type="submit" value="Submit"/>
        </form>

<form method="post" action="Test_Page.php">
    <input type="hidden" name="form2">
    <input type="submit" id="deleteBtn" value="Delete"/>
    <div class="nav" id="inst1">    
        <table class="one">                 
            <thead>
                <tr>
                    <th scope="col"></th>
                    <th scope="col">Active</th>
                    <th scope="col">Data</th>
                    <th colspan="2">Mode1</th>
                    <th colspan="2">Mode2</th>
                </tr>
            </thead>                
<tbody> 

<?php               
    $req1 = "SELECT ID, Active, Data, Mode1, Mode2 FROM DATABASENAME" ;
    $result1 =  sqlsrv_query($con,$req1);
    while($row1 = sqlsrv_fetch_array($result1)) 
        {

        echo "<tr>";
        echo "<td><input type='checkbox' name = 'ID[]' value='" . $row1['ID'] . "'/></td>";
        echo '<td>'.$row1['Active'].'&nbsp;&nbsp;</td>
            <td>'.$row1['Data'].'&nbsp;&nbsp;</td>';                
                    if ($row1['Mode1']=='False'){
                        echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False" checked/></td>';
                        echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True"/></td>';
                        } else {
                        echo '<td>False : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="False"/></td>';
                        echo '<td>True : <input class="RadioBtn_Mode1" id="'.$row1['ID'].'" name="RadioBtn_Mode1'.$row1['ID'].'" type="radio" value="True" checked/></td>';
                        }
                    if ($row1['Mode2']=='False'){
                        echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False" checked/></td>';
                        echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True"/></td>';
                        } else {
                        echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False"  /></td>';
                        echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True" checked /></td>';
                        }

            echo    
            '</td>
            </tr>'; 

        }           
?>
</tbody>
        </table>

</div>  
</form>     
</body>
</html>

<?php
sqlsrv_close($con);
?>

I would like a master to tell me what I can do better and help me to turn radio buttons into check boxes for the data insert. SQL format table is BIT, so I want to insert true / false when I check/uncheck like I do with buttons. Thanks !

Willie Cheng
  • 7,679
  • 13
  • 55
  • 68
MasTech
  • 9
  • 3

1 Answers1

0

Without rewriting all of the code in the answer The idea of a checkbox, is marked or not. The input passed to the server by the browser, passes only if the checkbox is marked

So the the checkbox PHP code should check whether isset($_POST['RadioBtn_Mode2'] . $row1['ID']); A simple reference here Which is pretty much what you do already All that's left is changing the echoing of the html, basically dropping one tag per radio button, and changing the type to checkbox

For example, from this:

                echo '<td>False : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="False"  /></td>';
                echo '<td>True : <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="radio" value="True" checked /></td>';

To This:

               echo '<td>Check: <input class="RadioBtn_Mode2" id="'.$row1['ID'].'" name="RadioBtn_Mode2'.$row1['ID'].'" type="checkbox" value="False"  /></td>';

Once that works, changing the universal name RadioBtn to CheckBx (or to anything else) is esthetics, of course

Guy Louzon
  • 1,175
  • 9
  • 19
  • Thanks for your answer, I cannot turn 2 radio buttons into 1 checkbox ? Like checked = True, and uncheck = False, for each action it does the update into database as I do with buttons. – MasTech Jul 10 '19 at 12:12
  • You can, but then their names should be different, which makes your PHP more complicated, and you should use Javascript to force check and unchecking. Essentially turning the check boxes to radio buttons – Guy Louzon Jul 10 '19 at 12:34
  • If you'd like to change the radio button design, you can use css, without touching your backend or html. for example: https://www.w3schools.com/howto/howto_css_custom_checkbox.asp – Guy Louzon Jul 10 '19 at 12:36
  • Ok thanks, I'm going to check, the thing is that I have 4 radio buttons on the same line for Mode1 and Mode 2. I'd like to have 1 checkbox for Mode1 and 1 for Mode2. But as I understand it's not so easy as I have to javascript the check value and uncheck value. Looks like it's above my skills which are low. – MasTech Jul 10 '19 at 12:50